background preloader

2015/05/17第二輪

Facebook Twitter

RecyclerView使用详解(二) · Frank-Zhu. 25 Feb 2015 在上一篇(RecyclerView使用详解(一))文章中简单的介绍了RecyclerView的基本用法,接下来要来讲讲RecyclerView的更多用法,要实现不同的功能效果,大部分都还是在于RecyclerView的Adapter写法,所以我们着重来看看几种不同功能的Adapter写法。 ##一、多Item布局实现(MultipleItem) 如果之前你用过ListView实现过此功能,那么你一定对下面这两个方法并不陌生 @Override public int getItemViewType(int position) { return super.getItemViewType(position); } @Override public int getViewTypeCount() { return super.getViewTypeCount(); } 其中getItemViewType方法是用来获取当前项Item(position参数)是哪种类型的布局,getViewTypeCount方法是用来获取当前listview总共有多少种类型的布局。 如果你用RecyclerView,你会发现getViewTypeCount这个方法没有了,只有一个getItemViewType方法,用法和listview没有任何区别,这里要注意的就是这个函数onCreateViewHolder(ViewGroup parent, int viewType)这里的第二个参数就是View的类型,可以根据这个类型判断去创建不同item的ViewHolder。 怎么样,是不是很简单,没错,就是这么简单,但是只要会了这个多Item,那么你就可以做出很多效果了,比如HeaderView和BottomView也是可以通过这种方式来实现的。

##二、效果图如下: 最后给出代码下载地址–>Demo Code RecyclerView使用详解(一) RecyclerView使用详解(三) ImageViewHolder - Google �j�M. Android調用手機拍照以及從相冊選擇照片. Android Camera系列开发 (一): 通过Intent拍照 - 雨水的专栏. Android Camera系列开发 (一): 通过Intent拍照 作者:胡家辉 2013-6-21 CSDN博客: 概述 使用Camera有两种方式:通过Intent使用已有的app和通过Camera构建自己的app。 Camera相关声明 如果你的应用程序要使用Camera,则必须获得使用许可,需要在AndroidManifest.xml中加入如下声明。 如果你的应用程序必须有Camera才能使用,则声明如下: 否则应声明如下: 其他相关声明: 存储:<uses-permissionandroid:name="android.permission.WRITE_EXTERNAL_STORAGE" /> 语音:<uses-permissionandroid:name="android.permission.RECORD_AUDIO" /> 位置:<uses-permissionandroid:name="android.permission.ACCESS_FINE_LOCATION" /> 核心类 Itent:通过它进行Camera编程更简单。 Camera:通过它可以控制相机,构建功能更强大的Camera应用。 SurfaceView:提供实时预览功能。 通过Itent来实现拍照 第一步:在Eclipse中创建一个名为Photo的Android工程,可参见Hello world的例子; 第二步:在AndroidManifest.xml中添加使用Camera相关的声明如下: 第三步:编写MainActivity类。 (1) 添加成员变量 (2) 修改OnCreate方法 (3) 增加onActivityResult方法 (4) 新增两个辅助方法 类的源代码如下: 第四步:运行程序。

如果拍完照返回的时候,程序崩溃,查看日志出现如下错误: java.lang.RuntimeException: Failure delivering resultResultInfo{who=null, request=100, result=-1, data=null} to activity 在AndroidManifest.xml中的activity元素加入android:launchMode="singleInstance"属性即可解决该问题。 参考资料. Android編程: 實現Android照相機的拍照功能(Camera.takePicture) 前文描述了"Android的照相機預覽功能(Camera Preview)", 本文進一步新增照相機的拍照功能. 修改AndroidManifest.xml- 授予使用照相機權限, "android.permission.CAMERA"- 並設定屏幕在一定的方向, android:screenOrientation="portrait" 修改佈局(main.xml), 再多添加一個Button. 主要代碼, AndroidTakePicture.java主要是實踐ShutterCallback, 和PictureCallback, 調用takePicture()方法.本應用程序運行的結果是一個圖片的Bitmap位圖, bitmapPicture. package com.AndroidTakePicture; import java.io.IOException; import android.app.Activity;import android.graphics.Bitmap;import android.graphics.BitmapFactory;import android.graphics.PixelFormat;import android.hardware.Camera;import android.hardware.Camera.PictureCallback;import android.hardware.Camera.ShutterCallback;import android.os.Bundle;import android.view.SurfaceHolder;import android.view.SurfaceView;import android.view.View;import android.widget.Button; public class AndroidTakePicture extends Activity implements SurfaceHolder.Callback{ Camera myCamera;SurfaceView previewSurfaceView;SurfaceHolder previewSurfaceHolder;boolean previewing = false; 拍照後, 預覽顯示便會停止.

Android take picture intent - Google 搜尋. Android - Take and save picture on button press. Image - Taking pictures with camera android programmatically. Take a Picture and Save Using Camera App - Xamarin. Taking Pictures In An Android Fragment Using Intents. Taking Photos Simply. This lesson explains how to capture photos using an existing camera application.

Suppose you are implementing a crowd-sourced weather service that makes a global weather map by blending together pictures of the sky taken by devices running your client app. Integrating photos is only a small part of your application. You want to take photos with minimal fuss, not reinvent the camera. Happily, most Android-powered devices already have at least one camera application installed. In this lesson, you learn how to make it take a picture for you. Request Camera Permission If an essential function of your application is taking pictures, then restrict its visibility on Google Play to devices that have a camera. <manifest ... > <uses-feature android:name="android.hardware.camera" android:required="true" /> ... Take a Photo with the Camera App The Android way of delegating actions to other applications is to invoke an Intent that describes what you want done.

Static final int REQUEST_IMAGE_CAPTURE = 1; Android take picture example - Google 搜尋. GetActivity()为空的问题-Android开发问答-eoe 移动开发者论坛. Android-Fragment的使用getActivity() =null 解决方式 – IT985博客. Android. Fragment getActivity() sometime returns null. 智慧生活科技專業社群: Fragment與Activity間的資料傳遞. 上圖分別代表兩個Fragment,假如我們要寫出類似這個程式出來在資料的傳遞上該如何做呢? 以下提出兩個方法: 1. 當使用者點選新聞列表(Fragment 1)後資料直接傳遞到新聞內容(Fragment 2)顯示。 2. 為了重複使用Fragment UI元件,你應該建立一個完全獨立、模組化的佈局和行為的元件。 通常你會想要一個fragment與另一個資料傳遞,例如使用者事件來更新內容。 因此,從上述我們知道為了達到Fragment能重複使用,Google官網建議在Fragment程式寫法採用上述的第二種方式。 在Communicating with Other Fragments官網中的畫面右上方可以看到 Download the sample ,我們可以下載到檔名為FragmentBasics.zip的官網範例,而這篇中告訴我們為達到Fragment的資料傳遞必須: 1.Define an Interface 2.Implement the Interface 3.Deliver a Message to a Fragment 在定義接口(Define an Interface)部分,可以參考範例中HeadlinesFragment.java,我們可以從 onAttach()中看到定義了mCallback然後在程式上方定義了OnHeadlineSelectedListener接口方法作為與Activity間的資料傳遞使用。 然後使用onListItemClick()將事件傳送給Activity。 為了在MainActivity獲得上述的事件,必須實現接口(Implement the Interface),我們可以從MainActivity.java中看到implements HeadlinesFragment.OnHeadlineSelectedListener{.....

然後在onArticleSelected()中進行所接收的事件處理以及顯示文章。 至於Deliver a Message to a Fragment部分,在Fragment中可以透過getActivity()方法來取得所在的Activity元件中的相關資訊。 View listView =getActivity().findViewById(R.id.list); 然後選擇Finish就完成匯入程式了。 範例結果: 參考: Android-Fragment的使用 - 王子1987的专栏. 关于Fragment的使用,网上有很多关于它的文章。 一般的使用过程和生命周期之类的,就不在这讲述了,下附的demo也会对这些方面有所展示。 这里主要讲讲我在开发使用Fragment的过程中遇到的一些问题和使用时注意的要点。 首先要说的是,Fragment中有一个getActivity()的方法。 这个方法会返回当前Fragment所附加的Activity。 当fragment生命周期结束并销毁时,getActivity()返回的会是null。 所以在使用时要注意判断null或者捕获空指针异常。 但如果想要离开这个页面,但还保留这个页面,就要注意不要让它被回收了。 另外,还有一个问题,就是一些没有被回收的线程在执行时,如果fragment已经销毁,那么它上面的那些控件,包括getActivity()都将是个空值,这时候就要在线程里面增加处理。 当然,正常来说,如果这个线程是在fragment里面申请的那些线程,没有被当前fragment以外的对象引用,当该fragment被销毁的时候,接着也会把这个线程销毁,就好像销毁它的控件一样。 Fragment.getActivity() - Google 搜尋. OnPostCreate的片段OnPostCreate in FragmentIT怪 挨踢怪 28印象 二八印象. 看片段的生命周期。 等到片段是附加到活动 你可以在onActivtiyCreated做你的工作。 获得上下文使用getActivtiy() 引用文档 应用程序通常不会实现这个方法,它适用于系统类应用程序代码运行后做最后的初始化。

原文: Look at the Fragment Lifecycle. Wait till the fragment is attached to the activity You can do your work in onActivtiyCreated. To get Context use getActivtiy() Quoting docs Applications will generally not implement this method; it is intended for system classes to do final initialization after application code has run. protected void onPostCreate (Bundle savedInstanceState) Added in API level 1 Called when activity start-up is complete (after onStart() and onRestoreInstanceState(Bundle) have been called).** Applications will generally not implement this method; it is intended for system classes to do final initialization after application code has run.** Derived classes must call through to the super class's implementation of this method.

GetActivtiy Activity.finish - Google 搜尋. OnActivityResult不被称为片段onActivityResult not being called in FragmentIT怪 挨踢怪 28印象 二八印象. 原文: Option 1 : If you're calling startActivityForResult() from the fragment then you should call startActivityForResult() not getActivity().startActivityForResult(), as it will result in fragment onActivityResult(). If you're not sure where you're calling on startActivityForResult() and how you will be calling methods. Option 2: Since Activity gets the result of onActivityResult(), you will need to override the activity's onActivityResult() and call super.onActivityResult() to propagate to the respective fragment for unhandled results codes or for all. If above 2 options do not work, then refer option 3 as it will definitely work. Option 3 : Explicit call from fragment to onActivityResult function as follows In Parent Activity class, override the onActivityResult() method and even override the same in Fragment Class and call as the following code.

In Parent Class: In Child Class: @Overrideprotected void onActivityResult(int requestCode, int resultCode, Intent data) { } OnActivityResult not being called in Fragment. Option 1 : If ur calling startActivityForResult() from fragment Then u should call startActivityForResult() not getActivity().startActivityForResult() will result in fragment onActivityResult().

If not sure from where u r calling on startActivityForResult() and how ur calling methods. Option 2: Since Activity gets onActivityResult() result so need to override activity’s onActivityResult(), call super.onActivityResult() to propagate to respective fragment for unhandled results codes or for all. If above 2 options does not work then refer option 3 will definatly work. Option 3 : Explicit call fragment onActivityResult function as follows In Parent Activity class override onActivityResult() method and even override the same in Fragment Class and call as following code. In Parent Class: In Child Class: @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { //in fragment class callback } OnActivityResult not being called in Fragment - Android-Fragments - Android Puzzles(Questions & Answers)

Option 1 : If ur calling startActivityForResult() from fragment Then u should call startActivityForResult() not getActivity().startActivityForResult() will result in fragment onActivityResult(). If not sure from where u r calling on startActivityForResult() and how ur calling methods. Option 2: Since Activity gets onActivityResult() result so need to override activity's onActivityResult(), call super.onActivityResult() to propagate to respective fragment for unhandled results codes or for all.

If above 2 options does not work then refer option 3 will definatly work. Option 3 : Explicit call fragment onActivityResult function as follows In Parent Activity class override onActivityResult() method and even override the same in Fragment Class and call as following code. In Parent Class: In Child Class: @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { //in fragment class callback } Android - onActivityResult not being called in Fragment. 在Fragment里onActivityResult,startActivityForResult使用案例 - 综合讨论 - Android开发论坛 - 安卓开发论坛 - Android开发 - 安卓论坛 - 移动互联网门户 - Powered by Discuz!

Fragment() setOnClickListener(new Button.OnClickListener() public void onClick(View v) Intent intent = new Intent(); intent.setType("image/*"); intent.setAction(Intent.ACTION_GET_CONTENT); getActivity().startActivityForResult( Intent.createChooser(intent,