Android應用開發中使用Fragment的入門學習教程_Android

來源:互聯網
上載者:User

  Fragment是Android honeycomb 3.0開始新增的概念,Fragment名為片段不過卻和Activity十分相似,下面介紹下Android Fragment的作用和用法。Fragment用來描述一些行為或一部分使用者介面在一個Activity中,你可以合并多個fragment在一個單獨的activity中建立多個UI面板,同時重用fragment在多個activity中.你可以認為fragment作為一個activity中的一節模組 ,fragment有自己的生命週期,接收自己的輸入事件,你可以添加或移除從運行中的activity.

       一個fragment必須總是嵌入在一個activity中,同時fragment的生命週期受activity而影響,舉個例子吧,當activity 暫停,那麼所有在這個activity的fragments將被destroy釋放。然而當一個activity在運行比如resume時,你可以單獨的操控每個fragment,比如添加或刪除。

       Fragment作為Android 3.0的新特性,有些功能還是比較強大的,比如 合并兩個Activity,如圖

 如上圖所示,用Activity A 表示文章標題列表,ActivityB表示文章具體內容。我們可以看到兩個Activity通過兩個Fragment合并到一個Activity的布局方式,對於平板等大螢幕裝置來說有著不錯的展示面板。不過因為Fragment和Activity的生命週期都比較複雜,下圖表示的fragments的生命週期:

Activity、Fragment分別對比下:

兩個的生命週期很類似,也息息相關。
 
 
建立一個fragment你必須建立一個Fragment的子類或存在的子類,比如類似下面的代碼

public static class AndroidFragment extends Fragment { @Override public View onCreateView(LayoutInflater inflater, ViewGroup container,        Bundle savedInstanceState) {     return inflater.inflate(R.layout.android_fragment, container, false); }}

 

onCreate()
當fragment建立時被調用,你應該初始化一些實用的組件,比如在fragment暫停或停止時需要恢複的

onCreateView()
當系統調用fragment在首次繪製使用者介面時,如果畫一個UI在你的fragment你必須返回一個View當然了你可以返回null代表這個fragment沒有UI.

那麼如何添加一個Fragment到Activity中呢? Activity的布局可以這樣寫

<?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.android.cwj.ArticleListFragment"   android:id="@+id/list"   android:layout_weight="1"   android:layout_width="0dp"   android:layout_height="match_parent" /> <fragment android:name="com.android.cwj.ArticleReaderFragment"   android:id="@+id/viewer"   android:layout_weight="2"   android:layout_width="0dp"   android:layout_height="match_parent" /></LinearLayout>

 通常地 fragment做為宿主activity UI的一部分, 被作為activity整個view hierarchy的一部分被嵌入. 有2種方法你可以添加一個fragment到activity layout:

一、在activity的layout檔案中聲明fragment
      你可以像為View一樣, 為fragment指定layout屬性(sdk3.0以後).
      例子是一個有2個fragment的activity:

<?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>

<fragment> 中的 android:name 屬性指定了在layout中執行個體化的Fragment類.

當系統建立這個activity layout時, 它執行個體化每一個在layout中指定的fragment,並調用每一個上的onCreateView()方法,來擷取每一個fragment的layout. 系統將從fragment返回的 View 直接插入到<fragment>元素所在的地方.

注意: 每一個fragment都需要一個唯一的標識, 如果activity重啟,系統可以用來恢複fragment(並且你也可以用來捕獲fragment來處理事務,例如移除它.)

有3種方法來為一個fragment提供一個標識:

  • 為 android:id 屬性提供一個唯一ID.
  • 為 android:tag 屬性提供一個唯一字串.
  • 如果以上2個你都沒有提供, 系統使用容器view的ID.

二、使用FragmentManager將fragment添加到一個已存在的ViewGroup.

       當activity啟動並執行任何時候, 都可以將fragment添加到activity layout.只需簡單的指定一個需要放置fragment的ViewGroup.為了在你的activity中操作fragment事務(例如添加,移除,或代替一個fragment),必須使用來自 FragmentTransaction 的API.

可以按如下方法,從你的Activity取得一個 FragmentTransaction 的執行個體:

FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();

然後你可以使用 add() 方法添加一個fragment, 指定要添加的fragment, 和要插入的view.

ExampleFragment fragment = new ExampleFragment(); fragmentTransaction.add(R.id.fragment_container, fragment); fragmentTransaction.commit();

      add()的第一個參數是fragment要放入的ViewGroup, 由resource ID指定, 第二個參數是需要添加的fragment.一旦用FragmentTransaction做了改變,為了使改變生效,必須調用commit().

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.