1,什麼情況下使用
fragment通常用來作爲一個activity的用戶界面的一部分
例如, 一個新聞應用可以在屏幕左側使用一個fragment來展示一個文章的列表,然後在屏幕右側使用另一個fragment來展示一篇文章 – 2個fragment並排顯示在相同的一個activity中,並且每一個fragment擁有它自己的一套生命週期回調方法,並且處理它們自己的用戶輸入事件. 因此, 取代使用一個activity來選擇一篇文章,而另一個activity來閱讀文章 的方式,用戶可以在相同的activity中選擇一篇文章並且閱讀。
http://my.oschina.net/u/573470/blog/73333(原文出處)
創建Fragment
要創建一個fragment, 必須創建一個 Fragment 的子類 (或者繼承自一個已存在的它的子類). Fragment類的代碼看起來很像 Activity .它包含了和activity類似的回調方法, 例如 onCreate(),
onStart(),onPause, 以及 onStop(). 事實上, 如果你準備將一個現成的Android應用轉換到使用fragment,你可能只需簡單的將代碼從你的activity的回調函數分別移動到你的fragment的回調方法.
通常, 應當至少實現如下的生命週期方法:
- onCreate()
當創建fragment時, 系統調用此方法.
在實現代碼中,應當初始化想要在fragment中保持的必要組件, 當fragment被暫停或者停止後可以恢復. - onCreateView()
fragment第一次繪製它的用戶界面的時候, 系統會調用此方法. 爲了繪製fragment的UI,此方法必須返回一個View, 這個view是你的fragment佈局的根view. 如果fragment不提供UI, 可以返回null. - onPause()
用戶將要離開fragment時,系統調用這個方法作爲第一個指示(然而它不總是意味着fragment將被銷燬.) 在當前用戶會話結束之前,通常應當在這裏提交任何應該持久化的變化(因爲用戶有可能不會返回).
- DialogFragment
顯示一個浮動的對話框.
用這個類來創建一個對話框,是使用在Activity類的對話框工具方法之外的一個好的選擇,
因爲你可以將一個fragment對話框合併到activity管理的fragment back stack中,允許用戶返回到一個之前曾被摒棄的fragment. - ListFragment
顯示一個由一個adapter(例如 SimpleCursorAdapter)管理的項目的列表, 類似於ListActivity.
它提供一些方法來管理一個list view, 例如 onListItemClick()回調來處理點擊事件. - PreferenceFragment
顯示一個 Preference對象的層次結構的列表, 類似於PreferenceActivity.
這在爲你的應用創建一個"設置"activity時有用處.
- 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的之前的實例的數據
將fragment添加到activity
通常地, fragment爲宿主activity提供UI的一部分, 被作爲activity的整個viewhierarchy的一部分被嵌入. 有2種方法你可以添加一個fragment到activitylayout:
在activity的layout文件中聲明fragment
你可以像爲View一樣, 爲fragment指定layout屬性.
例子是一個有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>
當系統創建這個activity layout時,它實例化每一個在layout中指定的fragment,並調用每一個上的onCreateView()方法,來獲取每一個fragment的layout.系統將從fragment返回的 View直接插入到<fragment>元素所在的地方.
注意 : 每一個fragment都需要一個唯一的標識,如果activity重啓,系統可以用來恢復fragment(並且你也可以用來捕獲fragment來處理事務,例如移除它.)
有3種方法來爲一個fragment提供一個標識:
- 爲 android:id 屬性提供一個唯一ID.
- 爲 android:tag 屬性提供一個唯一字符串.
- 如果以上2個你都沒有提供, 系統使用容器view的ID.
撰寫代碼將fragment添加到一個已存在的ViewGroup.
當activity運行的任何時候, 都可以將fragment添加到activitylayout.只需簡單的指定一個需要放置fragment的ViewGroup.爲了在你的activity中操作fragment事務(例如添加,移除,或代替一個fragment),必須使用來自FragmentTransaction 的API.
可以按如下方法,從你的Activity取得一個 FragmentTransaction 的實例:
- FragmentManager fragmentManager = getFragmentManager();
- FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
- ExampleFragment fragment = new ExampleFragment();
- fragmentTransaction.add(R.id.fragment_container, fragment);
- fragmentTransaction.commit();
管理Fragment
要在activity中管理fragment,需要使用FragmentManager.
通過調用activity的getFragmentManager()取得它的實例.
可以通過FragmentManager做一些事情, 包括:
- 使用findFragmentById() (用於在activitylayout中提供一個UI的fragment)或findFragmentByTag()(適用於有或沒有UI的fragment)獲取activity中存在的fragment
- 將fragment從後臺堆棧中彈出, 使用 popBackStack() (模擬用戶按下BACK 命令).
- 使用addOnBackStackChangeListener()註冊一個監聽後臺堆棧變化的listener.
處理Fragment事務
關於在activity中使用fragment的很強的一個特性是:根據用戶的交互情況,對fragment進行添加,移除,替換,以及執行其他動作.提交給activity的每一套變化被稱爲一個事務,可以使用在 FragmentTransaction 中的 API 處理.我們也可以保存每一個事務到一個activity管理的backstack,允許用戶經由fragment的變化往回導航(類似於通過activity往後導航).
從 FragmentManager 獲得一個FragmentTransaction的實例 :
- FragmentManager fragmentManager = getFragmentManager();
- FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
在調用commit()之前, 你可能想調用 addToBackStack(),將事務添加到一個fragment事務的backstack. 這個back stack由activity管理, 並允許用戶通過按下 BACK按鍵返回到前一個fragment狀態.
舉個例子, 這裏是如何將一個fragment替換爲另一個, 並在後臺堆棧中保留之前的狀態:
- // Create new fragment and transaction
- Fragment newFragment = new ExampleFragment();
- FragmentTransaction transaction = getFragmentManager().beginTransaction();
- // Replace whatever is in the fragment_container view with this fragment,
- // and add the transaction to the back stack
- transaction.replace(R.id.fragment_container, newFragment);
- transaction.addToBackStack(null);
- // Commit the transaction
- transaction.commit();
如果添加多個變化到事務(例如add()或remove())並調用addToBackStack(),然後在你調用commit()之前的所有應用的變化會被作爲一個單個事務添加到後臺堆棧, BACK按鍵會將它們一起回退.