注:本文翻譯自Google官方的Android Developers Training文檔,譯者技術(shù)一般,由于喜愛安卓而產(chǎn)生了翻譯的念頭,純屬個人興趣愛好。
原文鏈接: http://developer.android.com/training/basics/fragments/creating.html
你可以把一個fragment看成是一個activity的模塊,有著自己的生命周期,接收自己的時間輸入,你可以在activity的運(yùn)行階段添加或者移除fragment(某種意義上說,像是你可以在不同activity中重用的子activity)。這堂課將會展示如何通過使用 Support Library 來繼承 Fragment 類,這樣一來,你的應(yīng)用就能與Android 1.6及以上的設(shè)備兼容了。
Note:
如果你決定你的應(yīng)用僅支持API Level 11及以上的系統(tǒng),那么你無需使用 Support Library ,可以使用框架中內(nèi)置的 Fragment 類和與它相關(guān)的APIs。不過要注意這堂課關(guān)注的是使用 Support Library 中提供的API,它和平臺中內(nèi)置的 Fragment 類相比,區(qū)別在于兩者的包簽名不同,有時候還有一些接口名字上的差異。
在你開始學(xué)習(xí)這節(jié)課之前,你必須合理配置你的Android項(xiàng)目來使用 Support Library。如果在此之前你沒有使用過 Support Library,那么按照 Support Library Setup 這一文檔的步驟,配置你的項(xiàng)目來使用 v4 庫。然而,你也可以使用“ v7應(yīng)用兼容庫( v7 appcompat library ) ”使你的activities包含 action bar ,此時你的應(yīng)用將兼容Android 2.1(API Level 7)及以上的系統(tǒng),同時也包含了 Fragment 的APIs。
?
一). 創(chuàng)建一個Fragment類
為了創(chuàng)建一個fragment,繼承 Fragment 類,之后覆寫核心生命周期函數(shù)來插入你的應(yīng)用邏輯,這和你處理 Activity 類的方法很相似。
當(dāng)創(chuàng)建一個
Fragment
時,有一點(diǎn)不同的地方是:你必須使用
onCreateView()
回調(diào)函數(shù)來定義它的布局。事實(shí)上,這是唯一一個為了使一個
fragment
運(yùn)行所需要的回調(diào)函數(shù)。下面是一個簡單的例子:
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需要實(shí)現(xiàn)其他的生命周期函數(shù),這允許你去管理它的狀態(tài)(從activity添加或移除時,或者activity自身聲明周期狀態(tài)發(fā)生轉(zhuǎn)變時)。例如,當(dāng)activity調(diào)用了
onPause()
方法,所有activity中的fragment也將調(diào)用
onPause()
方法。
可以閱讀 Fragments 獲取更多關(guān)于fragment生命周期和回調(diào)函數(shù)的知識。
?
二). 使用XML將Fragment添加至一個Activity
盡管fragments是可重用、模塊化的UI組件,每個fragment的實(shí)例必須和一個父 FragmentActivity 關(guān)聯(lián)。你可以通過在activity的XML布局文件中定義每一個fragment來實(shí)現(xiàn)這種關(guān)聯(lián)。
Note:
FragmentActivity 是一個由 Support Library 提供的 特殊 activity,用來處理早于API Level 11系統(tǒng)中的fragment。如果你所支持的系統(tǒng)版本高于API Level 11,那么你可以直接用常規(guī)的 Activity 。
下面是一個布局文件的例子,它向一個activity添加了兩個fragment,前提是設(shè)備屏幕可認(rèn)為是“大( 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:
關(guān)于更多為不同屏幕尺寸創(chuàng)建布局的知識,可以閱讀: Supporting Different Screen Sizes 。
之后將布局應(yīng)用到你的activity當(dāng)中:
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應(yīng)該繼承 ActionBarActivity ,它是 FragmentActivity 的子類。 更多信息可以閱讀: Adding the Action Bar (博客鏈接: http://www.cnblogs.com/jdneo/p/3440367.html )。
Note:
當(dāng)你通過將fragment在XML布局文件中定義的方式把fragment添加到activity中,你不能再運(yùn)行時移除這個fragment。如果你計(jì)劃在用戶交互過程中可以吧fragment換入或換出,你必須在activity第一次啟動的時候把這個fragment添加到activity中,這是下節(jié)課將會展示的內(nèi)容。
更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主
微信掃碼或搜索:z360901061

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