Android Fragment詳解(三): 實現Fragment的界面

爲fragment添加用戶界面:

    Fragment一般作爲activity的用戶界面的一部分,把它自己的layout嵌入到activity的layout中。 一個

    要爲fragment提供layout,你必須實現onCreateView()回調方法,然後在這個方法中返回一個View對象,這個對象是fragment的layout的根。

    注:如果你的fragment是從ListFragment中派生的,就不需要實現onCreateView()方法了,因爲默認的實現已經爲你返回了ListView控件對象。

    要從onCreateView()方法中返回layout對象,你可以從layoutxml中生成layout對象。爲了幫助你這樣做,onCreateView()提供了一個LayoutInflater對象。

舉例:以下代碼展示了一個Fragment的子類如何從layoutxml文件example_fragment.xml中生成對象。

[java] view plaincopy
  1. publicstaticclassExampleFragmentextendsFragment{  
  2.    @Override  
  3.   publicViewonCreateView(LayoutInflaterinflater,ViewGroupcontainer,BundlesavedInstanceState){  
  4.        //Inflate the layout for this fragment  
  5.        returninflater.inflate(R.layout.example_fragment,container,false);  
  6.    }  
  7. }  


    onCreateView()參數中的container是存放fragment的layout的ViewGroup對象。savedInstanceState參數是一個Bundle,跟activity的onCreate()中Bundle差不多,用於狀態恢復。但是fragment的onCreate()中也有Bundle參數,所以此處的Bundle中存放的數據與onCreate()中存放的數據還是不同的。至於詳細信息,請參考“操控fragment的生命週期”一節。

Inflate()方法有三個參數:

1layout的資源ID。

2存放fragment的layout的ViewGroup。

3布爾型數據表示是否在創建fragment的layout期間,把layout附加到container上(在這個例子中,因爲系統已經把layout插入到container中了,所以值爲false,如果爲true會導至在最終的layout中創建多餘的ViewGroup(這句我看不明白,但我翻譯的應該沒錯))。

現在你看到如何爲fragment創建layout了,下面講述如何把它添加到activity中。

把fragment添加到activity

    一般情況下,fragment把它的layout作爲activitiy的loyout的一部分合併到activity中,有兩種方法將一個fragment添加到activity中:

方法一:在activity的layoutxml文件中聲明fragment

    如下代碼,一個activity中包含兩個fragment:

[java] view plaincopy
  1. <?xmlversion="1.0"encoding="utf-8"?>  
  2. <LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"  
  3.    android:orientation="horizontal"  
  4.    android:layout_width="match_parent"  
  5.    android:layout_height="match_parent">  
  6.    <fragmentandroid:name="com.example.news.ArticleListFragment"  
  7.            android:id="@+id/list"  
  8.            android:layout_weight="1"  
  9.            android:layout_width="0dp"  
  10.           android:layout_height="match_parent"/>  
  11.    <fragmentandroid:name="com.example.news.ArticleReaderFragment"  
  12.            android:id="@+id/viewer"  
  13.            android:layout_weight="2"  
  14.            android:layout_width="0dp"  
  15.           android:layout_height="match_parent"/>  
  16. </LinearLayout>  


<fragment>中聲明一個fragment

    當系統創建上例中的layout時,它實例化每一個fragment,然後調用它們的onCreateView()方法,以獲取每個fragment的layout。系統把fragment返回的view對象插入到<fragment>元素的位置,直接代替<fragment>元素。

    注:每個fragment都需要提供一個ID,系統在activity重新創建時用它來恢復fragment們,你也可以用它來操作fragment進行其它的事物,比如刪除它。有三種方法給fragment提供ID:

1 爲android:id屬性賦一個特定的標識符。
2 爲android:tag屬性賦一個標記名稱。

3如果你沒有使用上述任何一種方法,系統將使用fragment的容器的ID。

方法二:在代碼中添加fragment到一個ViewGroup

     這種方法可以在運行時,把fragment添加到activity的layout中。你只需指定一個要包含fragment的ViewGroup。

    爲了完成fragment的事務(比如添加,刪除,替換等),你必須使用FragmentTransaction的方法。你可以從activity獲取到FragmentTransaction,如下:

[java] view plaincopy
  1. FragmentManagerfragmentManager =getFragmentManager()  
  2. FragmentTransactionfragmentTransaction =fragmentManager.beginTransaction();  


    然後你可以用add()方法添加一個fragment,它有參數用於指定容納fragment的ViewGroup。如下:

[java] view plaincopy
  1. ExampleFragmentfragment =newExampleFragment();  
  2. fragmentTransaction.add(R.id.fragment_container,fragment);  
  3. fragmentTransaction.commit();  


    Add()的第一個參數是容器ViewGroup,第二個是要添加的fragment。一旦你通過FragmentTransaction對fragment做出了改變,你必須調用方法commit()提交這些改變。

不僅在無界面的fragment中,在有界面的fragment中也可以使用tag來作爲爲一標誌,這樣在需要獲取fragment對象時,要調用findFragmentTag()。

添加一個沒有界面的fragment

    上面演示瞭如何添加fragment來提供界面,然而,你也可以使用fragment爲activity提供後臺的行爲而不用顯示fragment的界面。

    要添加一個沒有界面的fragment,需在activity中調用方法add(Fragment,String)(它支持用一個唯一的字符串做爲fragment的”tag”,而不是viewID)。這樣添加的fragment由於沒有界面,所以你在實現它時不需調用實現onCreateView()方法。

    使用tag字符串來標識一個fragment並不是只能用於沒有界面的fragment上,你也可以把它用於有界面的fragment上,但是,如果一個fragment沒有界面,tag字符串將成爲它唯一的選擇。獲取以tag標識的fragment,需使用方法findFragmentByTab()。

發佈了59 篇原創文章 · 獲贊 81 · 訪問量 103萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章