注:本文翻譯自Google官方的Android Developers Training文檔,譯者技術一般,由于喜愛安卓而產生了翻譯的念頭,純屬個人興趣愛好。
原文鏈接: http://developer.android.com/training/basics/fragments/creating.html
你可以把一個fragment看成是一個activity的模塊,有著自己的生命周期,接收自己的時間輸入,你可以在activity的運行階段添加或者移除fragment(某種意義上說,像是你可以在不同activity中重用的子activity)。這堂課將會展示如何通過使用 Support Library 來繼承 Fragment 類,這樣一來,你的應用就能與Android 1.6及以上的設備兼容了。
Note:
如果你決定你的應用僅支持API Level 11及以上的系統,那么你無需使用 Support Library ,可以使用框架中內置的 Fragment 類和與它相關的APIs。不過要注意這堂課關注的是使用 Support Library 中提供的API,它和平臺中內置的 Fragment 類相比,區別在于兩者的包簽名不同,有時候還有一些接口名字上的差異。
在你開始學習這節課之前,你必須合理配置你的Android項目來使用 Support Library。如果在此之前你沒有使用過 Support Library,那么按照 Support Library Setup 這一文檔的步驟,配置你的項目來使用 v4 庫。然而,你也可以使用“ v7應用兼容庫( v7 appcompat library ) ”使你的activities包含 action bar ,此時你的應用將兼容Android 2.1(API Level 7)及以上的系統,同時也包含了 Fragment 的APIs。
?
一). 創建一個Fragment類
為了創建一個fragment,繼承 Fragment 類,之后覆寫核心生命周期函數來插入你的應用邏輯,這和你處理 Activity 類的方法很相似。
當創建一個
Fragment
時,有一點不同的地方是:你必須使用
onCreateView()
回調函數來定義它的布局。事實上,這是唯一一個為了使一個
fragment
運行所需要的回調函數。下面是一個簡單的例子:
import android.os.Bundle; import android.support.v4.app.Fragment; import android.view.LayoutInflater; import android.view.ViewGroup; public class ArticleFragment extends Fragment { @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { // Inflate the layout for this fragment return inflater.inflate(R.layout.article_view, container, false ); } }
像activity一樣,一個fragment需要實現其他的生命周期函數,這允許你去管理它的狀態(從activity添加或移除時,或者activity自身聲明周期狀態發生轉變時)。例如,當activity調用了
onPause()
方法,所有activity中的fragment也將調用
onPause()
方法。
可以閱讀 Fragments 獲取更多關于fragment生命周期和回調函數的知識。
?
二). 使用XML將Fragment添加至一個Activity
盡管fragments是可重用、模塊化的UI組件,每個fragment的實例必須和一個父 FragmentActivity 關聯。你可以通過在activity的XML布局文件中定義每一個fragment來實現這種關聯。
Note:
FragmentActivity 是一個由 Support Library 提供的 特殊 activity,用來處理早于API Level 11系統中的fragment。如果你所支持的系統版本高于API Level 11,那么你可以直接用常規的 Activity 。
下面是一個布局文件的例子,它向一個activity添加了兩個fragment,前提是設備屏幕可認為是“大( large )”的(通過在目錄名后面添加“ large ”適配符)。
res/layout-large/news_articles.xml
< LinearLayout xmlns:android ="http://schemas.android.com/apk/res/android" android:orientation ="horizontal" android:layout_width ="fill_parent" android:layout_height ="fill_parent" > < fragment android:name ="com.example.android.fragments.HeadlinesFragment" android:id ="@+id/headlines_fragment" android:layout_weight ="1" android:layout_width ="0dp" android:layout_height ="match_parent" /> < fragment android:name ="com.example.android.fragments.ArticleFragment" android:id ="@+id/article_fragment" android:layout_weight ="2" android:layout_width ="0dp" android:layout_height ="match_parent" /> </ LinearLayout >
Tip:
關于更多為不同屏幕尺寸創建布局的知識,可以閱讀: Supporting Different Screen Sizes 。
之后將布局應用到你的activity當中:
import android.os.Bundle; import android.support.v4.app.FragmentActivity; public class MainActivity extends FragmentActivity { @Override public void onCreate(Bundle savedInstanceState) { super .onCreate(savedInstanceState); setContentView(R.layout.news_articles); } }
如果使用的是 v7 appcompat library ,你的activity應該繼承 ActionBarActivity ,它是 FragmentActivity 的子類。 更多信息可以閱讀: Adding the Action Bar (博客鏈接: http://www.cnblogs.com/jdneo/p/3440367.html )。
Note:
當你通過將fragment在XML布局文件中定義的方式把fragment添加到activity中,你不能再運行時移除這個fragment。如果你計劃在用戶交互過程中可以吧fragment換入或換出,你必須在activity第一次啟動的時候把這個fragment添加到activity中,這是下節課將會展示的內容。
更多文章、技術交流、商務合作、聯系博主
微信掃碼或搜索:z360901061

微信掃一掃加我為好友
QQ號聯系: 360901061
您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對您有幫助就好】元
