android Fragments詳解三:實現Fragment的界面

轉載自 http://blog.csdn.net/niu_gao/article/details/7171697


fragment添加用戶界面

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

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

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

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

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

publicstaticclassExampleFragmentextendsFragment{
   
@Override
  
publicViewonCreateView(LayoutInflaterinflater,ViewGroupcontainer,BundlesavedInstanceState){
       
//Inflate the layout for this fragment
       
returninflater.inflate(R.layout.example_fragment,container,false);
   
}
}

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

Inflate()方法有三個參數:

1layout的資源ID

2存放fragmentlayoutViewGroup

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

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

fragment添加到activity

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

方法一:在activitylayoutxml文件中聲明fragment

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

<?xmlversion="1.0"encoding="utf-8"?>
<LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
   android:orientation="horizontal"
   android:layout_width="match_parent"
   android:layout_height="match_parent">
   <fragmentandroid:name="com.example.news.ArticleListFragment"
           android:id="@+id/list"
           android:layout_weight="1"
           android:layout_width="0dp"
          android:layout_height="match_parent"/>
   <fragmentandroid:name="com.example.news.ArticleReaderFragment"
           android:id="@+id/viewer"
           android:layout_weight="2"
           android:layout_width="0dp"
          android:layout_height="match_parent"/>
</LinearLayout>

<fragment>中聲明一個fragment

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

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

android:id屬性賦一個數字。

android:tag屬性賦一個字符串。

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

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

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

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

FragmentManagerfragmentManager =getFragmentManager()
FragmentTransactionfragmentTransaction =fragmentManager.beginTransaction();

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

ExampleFragmentfragment =newExampleFragment();
fragmentTransaction.add(R.id.fragment_container,fragment);
fragmentTransaction.commit();

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

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

添加一個沒有界面的fragment

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

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

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


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