Fragment 的使用(1)

基本: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實例的改變生效。

相關文章
相關標籤/搜索