基本:http://www.cnblogs.com/mengdd/archive/2013/01/08/2851368.htmlhtml
深刻:http://blog.csdn.net/pi9nc/article/details/12249619java
Fragment
android
Android是在Android 3.0 (API level 11)开始引入Fragment的。app
能够把Fragment想成Activity中的模块,这个模块有本身的布局,有本身的生命周期,单独处理本身的输入,在Activity运行的时候能够加载或者移除Fragment模块。ide
能够把Fragment设计成能够在多个Activity中复用的模块。布局
Fragment 生命周期this
由于Fragment必须嵌入在Acitivity中使用,因此Fragment的生命周期和它所在的Activity是密切相关的。spa
若是Activity是暂停状态,其中全部的Fragment都是暂停状态;若是Activity是stopped状态,这个Activity中全部的Fragment都不能被启动;若是Activity被销毁,那么它其中的全部Fragment都会被销毁。.net
可是,当Activity在活动状态,能够独立控制Fragment的状态,好比加上或者移除Fragment。设计
当这样进行fragment transaction(转换)的时候,能够把fragment放入Activity的back stack中,这样用户就能够进行返回操做。
Fragment 使用
使用Fragment时,须要继承Fragment或者Fragment的子类(DialogFragment, ListFragment, PreferenceFragment, WebViewFragment),因此Fragment的代码看起来和Activity的相似。
当建立包含Fragment的Activity时,若是用的是Support Library,那么继承的就应该是FragmentActivity而不是Activity。
必定要实现的三个方法
onCreate()
系统在建立Fragment的时候调用这个方法,这里应该初始化相关的组件,一些即使是被暂停或者被中止时依然须要保留的东西。
onCreateView()
当第一次绘制Fragment的UI时系统调用这个方法,必须返回一个View,若是Fragment不提供UI也能够返回null。
注意,若是继承自ListFragment,onCreateView()默认的实现会返回一个ListView,因此不用本身实现。
onPause()
当用户离开Fragment时第一个调用这个方法,须要提交一些变化,由于用户极可能再也不返回来。
实现 Fragment 的UI
提供fragment 的UI,必定要实现 onCreateView()方法。
假设将fragment 的布局写在example_fragment.xml中。
public static class ExampleFragment extends Fragment { @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { // Inflate the layout for this fragment return inflater.inflate(R.layout.example_fragment, container, false); } }
inflate()方法的三个参数:
第一个是resource ID,指明了当前的Fragment对应的资源文件;
第二个参数是父容器控件;
第三个布尔值参数代表是否链接该布局和其父容器控件,在这里的状况设置为false,由于系统已经插入了这个布局到父控件,设置为true将会产生多余的一个View Group。
将Fragment加入到Activity中
当 Fragment被 加入到 Activity中时,它会自在对应的ViewGroup中。
Fragment 有两种加载 方法,
一是,一种是在Activity的layout中使用标签<fragment>声明;此种状况 ,必定要在xml 文件中加入Fragment类的包名。
二是,在代码中将它加入到一个ViewGroup中。
加载 方法1 :经过 Activity布局文件将Fragment加入 Activity
在 Activity 布局文件中,将Fragment 做为 一个子标签 加入。
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="match_parent"> <fragment android:name="com.example.news.ArticleListFragment" android:id="@+id/list" android:layout_weight="1" android:layout_width="0dp" android:layout_height="match_parent" /> <fragment android:name="com.example.news.ArticleReaderFragment" android:id="@+id/viewer" android:layout_weight="2" android:layout_width="0dp" android:layout_height="match_parent" /> </LinearLayout>
注意 android:name应该写上fragment类的完整类名。
当系统建立这个 Activity的布局文件时,系统 会实例 化每个 Fragment ,并调用它们的 onCreateView()方法,来获得相应的 fragment布局,并将返回值插入到fragment 标签所在的地方 。
有三种方法为 fragment提供ID
android:id 属性,惟一 的id ,
android:tag属性,惟一 的字符串
若是 上面 两个都没有 ,则系统 用容器 view 的 id
加载方法2,经过代码 的方法将 fragment 加入 到一个 ViewGroup中
当Activity 处于running状态时,能够在Activity的布局中动态的加入 Fragment ,只要指定 加入 的这个 Fragment的 ViewGroup就行。
首先要一个 FragmentTransaction实例 。
FragmentManager fragmentManager = getFragmentManager( ); FragmentTransaction fragmentTransaction= fragmentManager.beginTransaction( );
(注,若是import android.support.v4.app.FragmentManager;那么使用的是:FragmentManager fragmentManager = getSupportFragmentManager();)
再用 add()方法加上 Fragment对象
ExampleFragment fragment= new ExampleFragment( ); fragmentTransaction.add( R.id.fragment_container, fragment); fragmentTransaction.commit();
其中第一个参数是这个fragment的容器,即父控件组。
最后须要调用commit()方法使得FragmentTransaction实例的改变生效。