最近事情很忙,一個新項目趕着出來,可是不少功能都要從新作,一直在編寫代碼、Debug。今天由於一個新程序要使用Fragment來作,雖然之前也使用過Fragment,不過沒有仔細研究,今天順道寫篇文章記錄一下Fragment的使用。這文章主要參考了Android官網的介紹。html
Fragment是Android3.0後增長的新控件,有點相似於Activity組件,也是用來承載各類View元素。Google增長這個玩意的目的是爲了平板電腦裏面能夠複用部分顯示的View,只要寫好一個View,能夠同時在手機和平板等不一樣尺寸的設備上使用。並且這個轉換過程系統幫你搞定了。下面咱們分類說說Fragment的使用。android
(PS:新建的QQ羣,有興趣能夠加入一塊兒討論:Android羣:322599434)設計模式
一、爲什麼使用Fragmentide
下面是Android官網爲了說明Fragment做用的例子:一個新聞應用能夠在屏幕左側使用一個fragment來展現一個文章的列表,而後在屏幕右側使用另外一個fragment來展現一篇文章--2個fragment並排顯示在相同的一個activity中,而且每個fragment擁有它本身的一套生命週期回調方法,而且處理它們本身的用戶輸入事件。 所以, 取代使用一個activity來選擇一篇文章而另外一個activity來閱讀文章的方式,用戶能夠在同一個activity中選擇一篇文章而且閱讀, 如圖所示:函數
當運行在一個特別大的屏幕時(例如平板電腦),應用能夠在Activity A中嵌入2個fragment。可是若是在一個正常尺寸的屏幕(例如手機)上,沒有足夠的空間同時供2個fragment用, 所以, Activity A會僅包含文章列表的fragment, 而當用戶選擇一篇文章時, 它會啓動ActivityB,它包含閱讀文章的fragment.所以, 應用能夠同時支持上圖中的2種設計模式。工具
fragment是一個爲了界面視圖能夠重用的組件,由於fragment定義了它本身的佈局, 以及經過使用它本身的生命週期回調方法定義了它本身的行爲,你能夠將fragment包含到多個activity中. 這點特別重要, 由於這容許你將你的用戶體驗適配到不一樣的屏幕尺寸.舉個例子,你可能會僅當在屏幕尺寸足夠大時,在一個activity中包含多個fragment,而且,當不屬於這種狀況時,會啓動另外一個單獨的,使用不一樣fragment的activity。
佈局
二、建立Fragmentui
要建立一個Fragment, 必須建立一個 Fragment 的子類 (或者繼承自一個已存在的它的子類)。Fragment類的代碼看起來很像 Activity 。它包含了和activity相似的回調方法, 例如onCreate()、 onStart()、onPause()以及 onStop()。事實上, 若是你準備將一個現成的Android應用轉換到使用fragment,可能只需簡單的將代碼從你的activity的回調方法分別移動到你的fragment的回調方法便可。
下面咱們看看Fragment的子類有哪些:this
三、Fragment生命週期spa
下面是Fragment的生命週期圖,摘自Android官網。
繼承了Fragment相關類以後,咱們須要重寫幾個回調函數,實現相關功能,下面面3個函數是咱們通常都會重寫的回調方法:
//Edited by mythou
//http://www.cnblogs.com/mythou/
public class MainFragment extends Fragment { //建立Fragment public void onCreate() {} //返回View給Activity使用 public View onCreateView() {} public void onPause() {} }
除了這些,對比上面的生命週期,還能夠看到其餘的回調方法。咱們能夠根據須要重寫相關方法。
四、建立Fragment
fragment一般用來做爲一個activity的用戶界面的一部分,並將它的layout提供給activity。爲了給一個fragment提供一 個layout,你必須實現 onCreateView()回調方法, 當到了fragment繪製它本身的layout的時候,Android系統調用它。你的此方法的實現代碼必須返回一個你的fragment的 layout的根view。
另外,若是你的fragment是ListFragment的子類,它的默認實現是返回從onCreateView()返回一個ListView,因此通常狀況下沒必要實現它。
從onCreateView()返回的View, 也能夠從一個layout的xml資源文件中讀取並生成。爲了幫助你這麼作, onCreateView() 提供了一個LayoutInflater 對象。
下面看個例子,從XML加載視圖View,跟咱們通常的View裏面動態加載解析XML生成View同樣。
//Edited by mythou
//http://www.cnblogs.com/mythou/
public static class ExampleFragment extends Fragment { @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { // Inflate the layout for this fragment return inflater.inflate(R.layout.example_fragment, container, false); } }
傳入onCreateView()的container參數是你的fragmentlayout將被插入的父ViewGroup(來自activity的layout) savedInstanceState 參數是一個Bundle, 若是fragment是被恢復的,它提供關於fragment的以前的實例的數據,inflate() 方法有3個參數:
五、把Fragment添加到Activity
一般地, fragment爲宿主activity提供UI的一部分, 被做爲activity的整個viewhierarchy的一部分被嵌入, 有2種方法你能夠添加一個fragment到activity layout:
在activity的layout文件中聲明fragment:
在這種狀況下,你能夠像爲View同樣, 爲fragment指定layout屬性.例子是一個有2個fragment的activity的layout:
//Edited by mythou
//http://www.cnblogs.com/mythou/
<?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提供一個標識:
當activity運行的任什麼時候候, 均可以將fragment添加到activity layout。只需簡單的指定一個須要放置fragment的ViewGroup。爲了在你的 activity中操做fragment事務(例如添加,移除,或代替一個fragment),必須使用來自FragmentTransaction 的API。能夠按以下方法,從你的Activity取得一個 FragmentTransaction 的實例:
FragmentManager fragmentManager =getFragmentManager();
FragmentTransaction fragmentTransaction =fragmentManager.beginTransaction();
而後你可使用 add() 方法添加一個fragment, 指定要添加的fragment和要插入的view。
//Edited by mythou
//http://www.cnblogs.com/mythou/
ExampleFragment fragment = newExampleFragment(); fragmentTransaction.add(R.id.fragment_container,fragment); fragmentTransaction.commit();
add()的第一個參數是fragment要放入的ViewGroup, 由resource ID指定,第二個參數是須要添加的fragment。一旦用FragmentTransaction作了改變,爲了使改變生效,必須調用commit()。
參考:
http://developer.android.com/guide/components/fragments.html
Edited by mythou
原創博文,轉載請標明出處:http://www.cnblogs.com/mythou/p/3218813.html