源代碼參考:360雲盤中---本身的學習資料---Android總結過的項目---FragmentDemo.rar 本篇博客力求爲你們說明 Fragment 如何產生,什麼是 Fragment,Fragment 生命週期,如何靜態和動態的使用Fragment,Fragment 回退棧,Fragment 事務;以及 Fragment的一些特殊用途,例如:沒有佈局的Fragment有何用處?Fragment 如何與 Activity 交互?Fragment 如何建立對話框? Fragment 如何與 ActionBar 集成等等。 1、Fragment 的產生與介紹 Android 運行在各類各樣的設備中,有小屏幕的手機,超大屏的平板甚至電視。針對屏幕尺寸的差距,不少狀況下,都是先針對手機開發一套 App,而後拷貝一份,修改佈局以適應平板神馬超級大屏的。難道沒法作到一個 App 能夠同時適應手機和平板麼,固然了,必須有啊。Fragment 的出現就是爲了解決這樣的問題。你能夠把 Fragment 當成 Activity 的一個界面的一個組成部分,甚至 Activity 的界面能夠徹底有不一樣的 Fragment 組成,更帥氣的是 Fragment 擁有本身的生命週期和接收、處理用戶的事件,這樣就沒必要在 Activity 寫一堆控件的事件處理的代碼了。更爲重要的是,你能夠動態的添加、替換和移除某個 Fragment。 -------------------------------------------------------------------------------------------- 2、Fragment 的生命週期
能夠看到 Fragment 比 Activity 多了幾個額外的生命週期回調方法: 1.onAttach(Activity) 當 Fragment 與 Activity 發生關聯時調用。 2.onCreateView(LayoutInflater, ViewGroup,Bundle) 建立該 Fragment 的視圖 3.onActivityCreated(Bundle) 當 Activity 的 onCreate 方法返回時調用 4.onDestoryView() 與 onCreateView 想對應,當該 Fragment 的視圖被移除時調用 5.onDetach() 與 onAttach 相對應,當 Fragment 與 Activity 關聯被取消時調用 注意:除了 onCreateView,其餘的全部方法若是你重寫了,必須調用父類對於該方法的實現。 1. 當一個 Fragment 被建立的時候,它會經歷如下狀態. onAttach() onCreate() onCreateView() onActivityCreated() 2. 當這個 Fragment 對用戶可見的時候,它會經歷如下狀態。 onStart() onResume() 3. 當這個 Fragment 進入「後臺模式」的時候,它會經歷如下狀態。 onPause() onStop() 4. 當這個 Fragment 被銷燬了(或者持有它的 Activity 被銷燬了),它會經歷如下狀態。 onPause() onStop() onDestroyView() onDestroy() // 原本漏掉類這個回調,感謝xiangxue336提出。 onDetach() 5. 就像 Activitie 同樣,在如下的狀態中,可使用 Bundle 對象保存一個 Fragment 的對象。 onCreate() onCreateView() onActivityCreated() 6. Fragments 的大部分狀態都和 Activitie 很類似,但 Fragment 有一些新的狀態。 onAttached() —— 當fragment被加入到activity時調用(在這個方法中能夠得到所在的activity)。 onCreateView() —— 當activity要獲得fragment的layout時,調用此方法,fragment在其中建立本身的layout(界面)。 onActivityCreated() —— 當activity的onCreated()方法返回後調用此方法 onDestroyView() —— 當fragment中的視圖被移除的時候,調用這個方法。 onDetach() —— 當fragment和activity分離的時候,調用這個方法。 一旦 Activity 進入 resumed 狀態(也就是 running 狀態),你就能夠自由地添加和刪除 Fragment 了。所以,只有當 Activity 在 resumed 狀態時,Fragment 的生命週期才能獨立的運轉,其它時候是依賴於 Activity 的生命週期變化的。 -------------------------------------------------------------------------------------------- 3、靜態使用 Fragment 這是使用 Fragment 最簡單的一種方式,把 Fragment 當成普通的控件,直接寫在 Activity 的佈局文件中。步驟: 1. 繼承 Fragment,重寫 onCreateView 決定 Fragemnt 的佈局 2. 在 Activity 中聲明此 Fragment,就當和普通的 View 同樣 下面展現一個例子(我使用 2 個 Fragment 做爲 Activity 的佈局,一個 Fragment 用於標題佈局,一個 Fragment 用於內容佈局): <!-- 靜態 Fragment 標題佈局 --> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="45dp" android:background="@android:color/holo_green_light" > <ImageButton android:id="@+id/ibTitleLeftBtn" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerVertical="true" android:layout_marginLeft="15dip" android:background="@drawable/user" /> <TextView android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_alignParentTop="true" android:gravity="center" android:text="我不是微信" android:textColor="#fff" android:textSize="20sp" android:textStyle="bold" /> </RelativeLayout> <!-- 靜態 Fragment 內容佈局 --> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <TextView android:layout_width="fill_parent" android:layout_height="fill_parent" android:gravity="center" android:text="使用 Fragment 作主面板" android:textSize="20sp" android:textStyle="bold" /> </LinearLayout> /** *Copyright(C) 2014 Beijing Oradt Ltd. For Digital Visiting Card 1.0 *File Name:TitleFragment.java *Description:標題 Fragment *Author: Xiao JinLai *Date:2014-12-12下午4:07:52 *History: Date: Author: Depiction: */ public class TitleFragment extends Fragment implements OnClickListener { private ImageButton mLeftMenu; @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View tView=inflater.inflate(R.layout.fragment_title, container ,false); mLeftMenu=(ImageButton) tView.findViewById(R.id.ibTitleLeftBtn); mLeftMenu.setOnClickListener(this); return tView; } @Override public void onClick(View v) { switch (v.getId()) { case R.id.ibTitleLeftBtn: break; } } } /** *Copyright(C) 2014 Beijing Oradt Ltd. For Digital Visiting Card 1.0 *File Name:ContentFragment.java *Description:內容 Fragment *Author: Xiao JinLai *Date:2014-12-12下午4:24:56 *History: Date: Author: Depiction: */ public class ContentFragment extends Fragment { @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { return inflater.inflate(R.layout.fragment_content, container, false); } } <!-- 靜態 Fragment 佈局文件 --> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" > <fragment android:id="@+id/ftTitle" android:name="com.xjl.fragmentdemo.TitleFragment" android:layout_width="fill_parent" android:layout_height="45dp" /> <fragment android:id="@+id/ftContent" android:name="com.xjl.fragmentdemo.ContentFragment" android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_below="@id/ftTitle" /> </RelativeLayout> /** * @ClassName: StaticFragementActivity * @author Xiao JinLai * @Date 2014-12-13 下午8:38:52 * @Description:靜態 Fragement */ public class StaticFragementActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_static_fragment); } } 是否是把 Fragment 當成普通的 View 同樣聲明在 Activity 的佈局文件中,而後全部控件的事件處理等代碼都由各自的Fragment去處理,瞬間以爲Activity好乾淨有木有~~代碼的可讀性、複用性以及可維護性是否是瞬間提高了。 下面是效果圖
-------------------------------------------------------------------------------------------- 4、動態使用 Fragment 上面已經演示了,最簡單的使用 Fragment 的方式,下面介紹如何動態的添加、更新、以及刪除 Fragment 爲了動態使用 Fragment,咱們修改一下 Actvity 的佈局文件,中間使用一個 FrameLayout,下面添加四個按鈕,嘿嘿,不是微信的按鈕! <!-- 動態 Fragment 佈局文件 --> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <FrameLayout android:id="@+id/flContent" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" > </FrameLayout> <include layout="@layout/bottom_bar"/> </LinearLayout> <!-- 底部四個按鈕佈局 --> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/rlTabBottom" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:background="@drawable/bottom_bar" > <LinearLayout android:layout_width="fill_parent" android:layout_height="55dp" > <LinearLayout android:layout_width="0dp" android:layout_height="fill_parent" android:layout_weight="1" android:descendantFocusability="beforeDescendants" android:gravity="center" android:orientation="vertical" > <ImageButton android:id="@+id/ibTabBottomWeixin" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="#0000" android:clickable="false" android:src="@drawable/tab_weixin_pressed" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="微信" /> </LinearLayout> <LinearLayout android:layout_width="0dp" android:layout_height="fill_parent" android:layout_weight="1" android:descendantFocusability="beforeDescendants" android:gravity="center" android:orientation="vertical" > <ImageButton android:id="@+id/ibTabBottomFriend" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="#0000" android:clickable="false" android:src="@drawable/tab_find_frd_normal" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="朋友" /> </LinearLayout> <LinearLayout android:layout_width="0dp" android:layout_height="fill_parent" android:layout_weight="1" android:descendantFocusability="beforeDescendants" android:gravity="center" android:orientation="vertical" > <ImageButton android:id="@+id/ibTabBottomAddress" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="#0000" android:clickable="false" android:src="@drawable/tab_address_normal" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="通信錄" /> </LinearLayout> <LinearLayout android:layout_width="0dp" android:layout_height="fill_parent" android:layout_weight="1" android:descendantFocusability="beforeDescendants" android:gravity="center" android:orientation="vertical" > <ImageButton android:id="@+id/ibTabBottomSetting" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="#0000" android:clickable="false" android:src="@drawable/tab_settings_normal" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="設置" /> </LinearLayout> </LinearLayout> </RelativeLayout> /** * @ClassName: StaticFragementActivity * @author Xiao JinLai * @Date 2014-12-13 下午8:38:52 * @Description:動態 Fragement 主 Activity */ public class TrendsFragementActivity extends Activity implements OnClickListener { private WeiXinFragement mWeiXinFragement; private FriendFragement mFriendFragement; private AddressFragement mAddressFragement; private SettingsFragement mSettingsFragement; /** * 底部四個按鈕 */ private ImageButton mTabBtnWeixin; private ImageButton mTabBtnFrd; private ImageButton mTabBtnAddress; private ImageButton mTabBtnSettings; /** * 用於對 Fragment 進行管理 */ private FragmentManager mFragmentManager; @SuppressLint("NewApi") @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_trends_fragment); initViews(); mFragmentManager = getFragmentManager(); setTabSelection(0); } private void initViews() { mTabBtnWeixin = (ImageButton) findViewById(R.id.ibTabBottomWeixin); mTabBtnFrd = (ImageButton) findViewById(R.id.ibTabBottomFriend); mTabBtnAddress = (ImageButton) findViewById(R.id.ibTabBottomAddress); mTabBtnSettings = (ImageButton) findViewById(R.id.ibTabBottomSetting); mTabBtnWeixin.setOnClickListener(this); mTabBtnFrd.setOnClickListener(this); mTabBtnAddress.setOnClickListener(this); mTabBtnSettings.setOnClickListener(this); } @Override public void onClick(View v) { switch (v.getId()) { case R.id.ibTabBottomWeixin: setTabSelection(0); break; case R.id.ibTabBottomFriend: setTabSelection(1); break; case R.id.ibTabBottomAddress: setTabSelection(2); break; case R.id.ibTabBottomSetting: setTabSelection(3); break; } } /** * 根據傳入的index參數來設置選中的tab頁。 * */ @SuppressLint("NewApi") private void setTabSelection(int index) { // 重置按鈕 resetTabBtn(); // 開啓一個Fragment事務 FragmentTransaction tFTransaction = mFragmentManager.beginTransaction(); // 先隱藏掉全部的 Fragment,以防止有多個 Fragment 顯示在界面上的狀況 hideFragments(tFTransaction); switch (index) { case 0: // 當點擊了消息tab時,改變控件的圖片和文字顏色 mTabBtnWeixin.setImageResource(R.drawable.tab_weixin_pressed); if (mWeiXinFragement == null) { // 若是MessageFragment爲空,則建立一個並添加到界面上 mWeiXinFragement = new WeiXinFragement(); tFTransaction.add(R.id.flContent, mWeiXinFragement); } else { // 若是MessageFragment不爲空,則直接將它顯示出來 tFTransaction.show(mWeiXinFragement); } break; case 1: // 當點擊了消息tab時,改變控件的圖片和文字顏色 mTabBtnFrd.setImageResource(R.drawable.tab_find_frd_pressed); if (mFriendFragement == null) { // 若是MessageFragment爲空,則建立一個並添加到界面上 mFriendFragement = new FriendFragement(); tFTransaction.add(R.id.flContent, mFriendFragement); } else { // 若是MessageFragment不爲空,則直接將它顯示出來 tFTransaction.show(mFriendFragement); } break; case 2: // 當點擊了動態tab時,改變控件的圖片和文字顏色 mTabBtnAddress.setImageResource(R.drawable.tab_address_pressed); if (mAddressFragement == null) { // 若是NewsFragment爲空,則建立一個並添加到界面上 mAddressFragement = new AddressFragement(); tFTransaction.add(R.id.flContent, mAddressFragement); } else { // 若是NewsFragment不爲空,則直接將它顯示出來 tFTransaction.show(mAddressFragement); } break; case 3: // 當點擊了設置tab時,改變控件的圖片和文字顏色 mTabBtnSettings.setImageResource(R.drawable.tab_settings_pressed); if (mSettingsFragement == null) { // 若是SettingFragment爲空,則建立一個並添加到界面上 mSettingsFragement = new SettingsFragement(); tFTransaction.add(R.id.flContent, mSettingsFragement); } else { // 若是 SettingFragment 不爲空,則直接將它顯示出來 tFTransaction.show(mSettingsFragement); } break; } tFTransaction.commit(); } /** * 清除掉全部的選中狀態。 */ private void resetTabBtn() { mTabBtnWeixin.setImageResource(R.drawable.tab_weixin_normal); mTabBtnFrd.setImageResource(R.drawable.tab_find_frd_normal); mTabBtnAddress.setImageResource(R.drawable.tab_address_normal); mTabBtnSettings.setImageResource(R.drawable.tab_settings_normal); } /** * 將全部的Fragment都置爲隱藏狀態。 * * @param transaction * 用於對Fragment執行操做的事務 */ @SuppressLint("NewApi") private void hideFragments(FragmentTransaction transaction) { if (mWeiXinFragement != null) { transaction.hide(mWeiXinFragement); } if (mFriendFragement != null) { transaction.hide(mFriendFragement); } if (mAddressFragement != null) { transaction.hide(mAddressFragement); } if (mSettingsFragement != null) { transaction.hide(mSettingsFragement); } } } 能夠看到咱們使用 FragmentManager 對 Fragment 進行了動態的加載,這裏使用的是 replace 方法。下一節我會詳細介紹 FragmentManager 的經常使用 API。 注:若是使用 Android3.0 如下的版本,須要引入v4的包,而後 Activity 繼承 FragmentActivity,而後經過 getSupportFragmentManager得到 FragmentManager。不過仍是建議版Menifest文件的uses-sdk的 minSdkVersion 和 targetSdkVersion 都改成 11 以上,這樣就沒必要引入 v4 包了。 代碼中間還有 4 個 Fragment 的子類。這四個 Fragment 相似,先貼兩個看看。 /** * @ClassName: WeiXinFragement * @author Xiao JinLai * @Date 2014-12-21 下午8:09:01 * @Description:微信 Fragement */ public class WeiXinFragement extends Fragment { @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { return inflater.inflate(R.layout.fragement_weixin, container, false); } } /** * @ClassName: WeiXinFragement * @author Xiao JinLai * @Date 2014-12-21 下午8:09:01 * @Description:朋友 Fragement */ @SuppressLint("NewApi") public class FriendFragement extends Fragment { public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { return inflater.inflate(R.layout.fragement_friend, container, false); } } -------------------------------------------------------------------------------------------- 5、Fragment 家族經常使用的API 1.Fragment經常使用的三個類: android.app.Fragment 主要用於定義 Fragment android.app.FragmentManager 主要用於在 Activity 中操做 Fragment android.app.FragmentTransaction 保證一些列 Fragment 操做的原子性,熟悉事務這個詞,必定能明白 2.獲取FragmentManage的方式: getFragmentManager() // v4中,getSupportFragmentManager 3.主要的操做都是 FragmentTransaction 的方法: FragmentTransaction transaction = fm.benginTransatcion();//開啓一個事務 4.往 Activity 中添加一個 Fragment: transaction.add() 5.從Activity中移除一個Fragment,若是被移除的Fragment沒有添加到回退棧(回退棧後面會詳細說),這個Fragment實例將會被銷燬。 transaction.remove() 6.使用另外一個 Fragment 替換當前的,實際上就是 remove() 而後 add() 的合體 transaction.replace() 7.隱藏當前的 Fragment,僅僅是設爲不可見,並不會銷燬 transaction.hide() 8.顯示以前隱藏的 Fragment transaction.show() 9.將此 Fragment 從 Activity 中分離,會銷燬其佈局,但不會銷燬該實例 detach() 10.將從Activity中分離的Fragment,從新關聯到該Activity,從新建立其視圖層次 attach() 11.注意:經常使用Fragment的哥們,可能會常常遇到這樣 Activity 狀態不一致:State loss 這樣的錯誤。主要是由於:commit 方法必定要在 Activity.onSaveInstance() 以前調用。 transatcion.commit()//提交一個事務 -------------------------------------------------------------------------------------------- 上述,基本是操做 Fragment 的全部的方式了,在一個事務開啓到提交能夠進行多個的添加、移除、替換等操做。 值得注意的是:若是你喜歡使用 Fragment,必定要清楚這些方法,哪一個會銷燬視圖,哪一個會銷燬實例,哪一個僅僅只是隱藏,這樣才能更好的使用它們。 1.好比:我在 FragmentA 中的 EditText 填了一些數據,當切換到 FragmentB 時,若是但願會到 A 還能看到數據,則適合你的就是 hide 和 show;也就是說,但願保留用戶操做的面板,你可使用 hide 和 show,固然了不要使勁在那 new 實例,進行下非 null 判斷。 2.再好比:我不但願保留用戶操做,你可使用remove(),而後 add();或者使用 replace() 這個和 remove,add 是相同的效果。 3.remove 和 detach 有一點細微的區別,在不考慮回退棧的狀況下,remove 會銷燬整個 Fragment 實例,而detach 則只是銷燬其視圖結構,實例並不會被銷燬。那麼兩者怎麼取捨使用呢?若是你的當前 Activity 一直存在,那麼在不但願保留用戶操做的時候,你能夠優先使用 detach。 上述已經介紹完成了Fragment經常使用的一些方法,相信看完,你們必定清楚了Fragment的產生理由,以及如何使用Fragment,再根據API的講解,也能明白,曾經爲什麼以爲Fragment會出現一些列亂七八槽的問題,終究是由於沒有弄清楚其生命週期。 因爲篇幅緣由,剩下的內容留到下一篇了。在下一篇,會介紹: 1、如何管理 Fragment 回退棧 2、Fragment 如何與 Activity 交互 3、Fragment 與 Activity 交互的最佳實踐 4、沒有視圖的 Fragment 的用處 5、使用 Fragment 建立對話框 六、如何與 ActionBar,MenuItem 集成等