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

來源:互聯網
上載者:User

為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中產生對象。

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

    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:

<?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()方法,以擷取每個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,如下:

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

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

ExampleFragmentfragment =newExampleFragment();
fragmentTransaction.add(R.id.fragment_container,fragment);
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()。

 

摘自 nkmnkm的專欄

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.