Android中Fragment的应用
?
Fragment 表现 Activity 中用UI的一个行为或者一部分. 可以组合多个fragment放在一个单独的activity中来创建一个多界面区域的UI,并可以在多个activity里重用某一个fragment.把fragment 想象成一个activity的模块化区域, 有它自己的生命周期, 接收属于它的输入事件, 并且可以在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>
FragmentManager fragmentManager = getFragmentManager();?FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
ExampleFragment fragment =?new?ExampleFragment(); fragmentTransaction.add(R.id.fragment_container, fragment);?fragmentTransaction.commit();add()的第一个参数是fragment要放入的ViewGroup, 由resource ID指定, 第二个参数是需要添加的fragment.一旦用FragmentTransaction做了改变,为了使改变生效,必须调用commit().?
FragmentManager fragmentManager = getFragmentManager();?FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
// 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();
View listView = getActivity().findViewById(R.id.list);同样地, activity可以通过从FragmentManager获得一个到Fragment的引用来调用fragment中的方法, 使用 findFragmentById() 或 findFragmentByTag().
ExampleFragment fragment = (ExampleFragment) getFragmentManager().findFragmentById(R.id.example_fragment);
public?static?class FragmentA?extends?ListFragment {
?????...
?????// Container Activity must implement?this?interface
????public?interface?OnArticleSelectedListener {
?????????public?void onArticleSelected(Uri articleUri);
?????}
?????...?
?}然后fragment的宿主activity实现 OnArticleSelectedListener 接口, 并覆写 onArticleSelected() 来通知fragment B,从fragment A到来的事件.为了确保宿主activity实现这个接口, fragment A的 onAttach() 回调方法(当添加fragment到activity时由系统调用) 通过将作为参数传入onAttach()的Activity做类型转换来实例化一个OnArticleSelectedListener实例.
public?static?class FragmentA?extends?ListFragment {
?????OnArticleSelectedListener mListener;
?????...
?????@Override
?????public?void onAttach(Activity activity) {
?????????super.onAttach(activity);
?????????try?{
?????????????mListener = (OnArticleSelectedListener) activity;
?????????}?catch?(ClassCastException e) {
?????????????throw?new?ClassCastException(activity.toString() +?" must implement OnArticleSelectedListener");
?????????}
?????}
?????...?
}
public?static?class FragmentA?extends?ListFragment {
?????OnArticleSelectedListener mListener;
?????...
?????@Override
?????public?void onListItemClick(ListView l, View v,?int?position,?long?id) {
?????????// Append the clicked item's row ID with the content provider Uri?
????????Uri noteUri = ContentUris.withAppendedId(ArticleColumns.CONTENT_URI, id);
?????????// Send the event and Uri to the host activity
????????mListener.onArticleSelected(noteUri);
?????}
?????...?
}