Fragment(片元)(二)(原文翻译)

一、管理Fragment

要管理fragment们,需使用FragmentManager,要获取它,需在activity中调用方法getFragmentManager()

你可以用FragmentManager来做以上事情:


1使用方法findFragmentById()findFragmentByTag(),获取activity中已存在的fragment们。

2使用方法popBackStack()activity的后退栈中弹出fragment们(这可以模拟后退键引发的动作)。

3用方法addOnBackStackChangedListerner()注册一个侦听器以监视后退栈的变化。


执行Fragment的事务

activity中使用fragment的一个伟大的好处是能跟据用户的输入对fragment进行添加、删除、替换以及执行其它动作的能力。你提交的一组fragment的变化叫做一个事务。事务通过FragmentTransaction来执行。你还可以把每个事务保存在activity的后退栈中,这样就可以让用户在fragment变化之间导航(跟在activity之间导航一样)。

你可以通过FragmentManager来取得FragmentTransaction的实例,如下:

[java]  view plain copy
  1. FragmentManager fragmentManager = getFragmentManager();  
  2. FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();  

一个事务是在同一时刻执行的一组动作(很像数据库中的事务)。你可以用add(),remove(),replace()等方法构成事务,最后使用commit()方法提交事务。

在调用commint()之前,你可以用addToBackStack()把事务添加到一个后退栈中,这个后退栈属于所在的activity。有了它,就可以在用户按下返回键时,返回到fragment们执行事务之前的状态。

如下例:演示了如何用一个fragment代替另一个fragment,同时在后退栈中保存被代替的fragment的状态。

[java]  view plain copy
  1. //Create new fragment and transaction  
  2. Fragment new Fragment = new ExampleFragment();  
  3. FragmentTransaction transaction=getFragmentManager().beginTransaction();  
  4.   
  5. //Replace whatever is in the fragment_container view with this fragment,  
  6. //and add the transaction to the backstack  
  7. transaction.replace(R.id.fragment_container,new Fragment);  
  8. transaction.addToBackStack(null);  
  9.   
  10. //Commit the transaction  
  11. transaction.commit();  

解释:new Fragment代替了控件IDR.id.fragment_container所指向的ViewGroup中所含的任何fragment。然后调用addToBackStack(),此时被代替的fragment就被放入后退栈中,于是当用户按下返回键时,事务发生回溯,原先的fragment又回来了

如果你向事务添加了多个动作,比如多次调用了add(),remove()等之后又调用了addToBackStack()方法,那么所有的在commit()之前调用的方法都被作为一个事务。当用户按返回键时,所有的动作都被反向执行(事务回溯)。

事务中动作的执行顺序可随意,但要注意以下两点:

1. 你必须最后调用commit()

2. 如果你添加了多个fragment,那么它们的显示顺序跟添加顺序一至(后显示的覆盖前面的)。

如果你在执行的事务中有删除fragment的动作,而且没有调用addToBackStack(),那么当事务提交时,那些被删除的fragment就被销毁了。反之,那些fragment就不会被销毁,而是处于停止状态。当用户返回时,它们会被恢复。

对于fragment事务,你可以应用动画。在commit()之前调用setTransition()就行。

但是,调用commit()后,事务并不会马上执行。它会在activityUI线程(其实就是主线程)中等待直到线程能执行的时候才执行(废话)。如果必要,你可以在UI线程中调用executePendingTransactions()方法来立即执行事务。但一般不需这样做,除非有其它线程在等待事务的执行。

警告:你只能在activity处于可保存状态的状态时,比如running中,onPause()方法和onStop()方法中提交事务,否则会引发异常。这是因为fragment的状态会丢失。如果要在可能丢失状态的情况下提交事务,请使用commitAllowingStateLoss()


二、Fragment与Activity通讯
与activity通讯

尽管fragment的实现是独立于activity的,可以被用于多个activity,但是每个activity所包含的是同一个fragment的不同的实例。

Fragment可以调用getActivity()方法很容易的得到它所在的activity的对象,然后就可以查找activity中的控件们(findViewById())。例如:

ViewlistView =getActivity().findViewById(R.id.list)。同样,你也可以getActivity().startActivity()这样来实现Activity的功能。同样的,activity也可以通过FragmentManager的方法查找它所包含的frament们。例如:

[java]  view plain copy
  1. ExampleFragment fragment =(ExampleFragment)getFragmentManager().findFragmentById(R.id.example_fragment  

activity响应fragment的事件

有时,你可能需要fragmentactivity共享事件。一个好办法是在fragment中定义一个回调接口,然后在activity中实现之

例如,还是那个新闻程序的例子,它有一个activityactivity中含有两个fragmentfragmentA显示新闻标题,fragmentB显示标题对应的内容。fragmentA必须在用户选择了某个标题时告诉activity,然后activity再告诉fragmentBfragmentB就显示出对应的内容(为什么这么麻烦?直接fragmentA告诉fragmentB不就行了?也可以啊,但是你的fragment就减少了可重用的能力。现在我只需把我的事件告诉宿主,由宿主决定如何处置,这样是不是重用性更好呢?)。如下例,OnArticleSelectedListener接口在fragmentA中定义

[java]  view plain copy
  1. public static class FragmentA extends ListFragment{  
  2.   ...  
  3.   //Container Activity must implement this interface  
  4.   public interface OnArticleSelectedListener{  
  5.       public void onArticleSelected(Uri articleUri);  
  6.   }  
  7.   ...  

然后activity实现接口OnArticleSelectedListener,在方法onArticleSelected()中通知fragmentB。当fragment添加到activity中时,会调用fragment的方法onAttach(),这个方法中适合检查activity是否实现了OnArticleSelectedListener接口,检查方法就是对传入的activity的实例进行类型转换,如下所示:

[java]  view plain copy
  1. public static class FragmentA extends ListFragment{  
  2.   OnArticleSelectedListener mListener;  
  3.   ...  
  4.   @Override  
  5.   public void onAttach(Activity activity){  
  6.       super.onAttach(activity);  
  7.       try{  
  8.           mListener =(OnArticleSelectedListener)activity;  
  9.       }catch(ClassCastException e){  
  10.           throw new ClassCastException(activity.toString()+"must implement OnArticleSelectedListener");  
  11.       }  
  12.   }  
  13.   ...  

如果activity没有实现那个接口,fragment抛出ClassCastException异常。如果成功了,mListener成员变量保存OnArticleSelectedListener的实例。于是fragmentA就可以调用mListener的方法来与activity共享事件。例如,如果fragmentA是一个ListFragment,每次选中列表的一项时,就会调用fragmentAonListItemClick()方法,在这个方法中调用onArticleSelected()来与activity共享事件,如下:

[java]  view plain copy
  1. public static class FragmentA extends ListFragment{  
  2.   OnArticleSelectedListener mListener;  
  3.   ...  
  4.   @Override  
  5.   public void onListItemClick(ListView l,View v,int position,long id){  
  6.       //Append the clicked item's row ID with the content provider Uri  
  7.       Uri noteUri =ContentUris.withAppendedId(ArticleColumns.CONTENT_URI,id);  
  8.       //Send the event and Uri to the host activity  
  9.       mListener.onArticleSelected(noteUri);  
  10.   }  
  11.   ...  

onListItemClick()传入的参数id是列表的被选中的行ID,另一个fragment用这个ID来从程序的ContentProvider中取得标题的内容。


三、来自官方的范例

任务:把条目添加到动作栏

你的fragment们可以向activity的菜单(按Manu键时出现的东西)添加项,同时也可向动作栏(界面中顶部的那个区域)添加条目,这都需通过实现方法onCreateOptionManu()来完成。

你从fragment添加到菜单的任何条目,都会出现在现有菜单项之后。Fragment之后可以通过方法onOptionsItemSelected()来响应自己的菜单项被选择的事件。

你也可以在fragemnt中注册一个view来提供快捷菜单(上下文菜单)。当用户要打开快捷菜单时,fragmentonCreateContextMenu()方法会被调用。当用户选择其中一项时,fragemntonContextItemSelected()方法会被调用。

注:尽管你的fragment可以分别收到它所添加的菜单项的选中事件,但是activity才是第一个接收这些事件的家伙,只有当activity对某个事件置之不理时,fragment才能接收到这个事件,对于菜单和快捷菜单都是这样。

下例中实验了之前所讲的所有内容。此例有一个activity,其含有两个fragment。一个显示莎士比亚剧的播放曲目,另一个显示选中曲目的摘要。此例还演示了如何跟据屏幕大小配置fragment

MainActivity:

[java]  view plain copy
  1. @Override    
  2. protectedvoid onCreate(Bundle savedInstanceState) {    
  3.    super.onCreate(savedInstanceState);    
  4.     
  5.    setContentView(R.layout.fragment_layout);    
  6. }   
Layout.xml:

[java]  view plain copy
  1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    
  2.     android:orientation="horizontal"    
  3.     android:layout_width="match_parent" android:layout_height="match_parent">    
  4.     
  5.     <fragment class="com.example.android.apis.app.FragmentLayout$TitlesFragment"    
  6.             android:id="@+id/titles" android:layout_weight="1"    
  7.             android:layout_width="0px" android:layout_height="match_parent" />    
  8.     
  9.     <FrameLayout android:id="@+id/details" android:layout_weight="1"    
  10.             android:layout_width="0px" android:layout_height="match_parent"    
  11.             android:background="?android:attr/detailsElementBackground" />    
  12.     
  13. </LinearLayout>    

系统在activity加载此layout时初始化TitlesFragment(用于显示标题列表),TitlesFragment的右边是一个FrameLayout,用于存放显示摘要的fragment,但是现在它还是空的,fragment只有当用户选择了一项标题后,摘要fragment才会被放到FrameLayout中。

然而,并不是所有的屏幕都有足够的宽度来容纳标题列表和摘要。所以,上述layout只用于横屏,现把它存放于ret/layout-land/fragment_layout.xml

之外,当用于竖屏时,系统使用下面的layout,它存放于ret/layout/fragment_layout.xml

[java]  view plain copy
  1. <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"    
  2.     android:layout_width="match_parent" android:layout_height="match_parent">    
  3.     <fragment class="com.example.android.apis.app.FragmentLayout$TitlesFragment"    
  4.             android:id="@+id/titles"    
  5.             android:layout_width="match_parent" android:layout_height="match_parent" />    
  6. </FrameLayout>   
这个 layout 只包含 TitlesFragment 。这表示当使用竖屏时,只显示标题列表。当用户选中一项时,程序会启动一个新的 activity 去显示摘要,而不是加载第二个 fragment

下一步,你会看到Fragment类的实现。第一个是TitlesFragment,它从ListFragment派生,大部分列表的功能由ListFragment提供。

当用户选择一个Title时,代码需要做出两种行为,一种是在同一个activity中显示创建并显示摘要fragment,另一种是启动一个新的activity

[java]  view plain copy
  1. public static class TitlesFragment extends ListFragment {    
  2.     boolean mDualPane;    
  3.     int mCurCheckPosition = 0;    
  4.     
  5.     @Override    
  6.     public void onActivityCreated(Bundle savedInstanceState) {    
  7.         super.onActivityCreated(savedInstanceState);    
  8.     
  9.         // Populate list with our static array of titles.    
  10.         setListAdapter(new ArrayAdapter<String>(getActivity(),    
  11.                 android.R.layout.simple_list_item_activated_1, Shakespeare.TITLES));    
  12.     
  13.         // Check to see if we have a frame in which to embed the details    
  14.         // fragment directly in the containing UI.    
  15.         View detailsFrame = getActivity().findViewById(R.id.details);    
  16.         mDualPane = detailsFrame != null && detailsFrame.getVisibility() == View.VISIBLE;    
  17.     
  18.         if (savedInstanceState != null) {    
  19.             // Restore last state for checked position.    
  20.             mCurCheckPosition = savedInstanceState.getInt("curChoice"0);    
  21.         }    
  22.     
  23.         if (mDualPane) {    
  24.             // In dual-pane mode, the list view highlights the selected item.    
  25.             getListView().setChoiceMode(ListView.CHOICE_MODE_SINGLE);    
  26.             // Make sure our UI is in the correct state.    
  27.             showDetails(mCurCheckPosition);    
  28.         }    
  29.     }    
  30.     
  31.     @Override    
  32.     public void onSaveInstanceState(Bundle outState) {    
  33.         super.onSaveInstanceState(outState);    
  34.         outState.putInt("curChoice", mCurCheckPosition);    
  35.     }    
  36.     
  37.     @Override    
  38.     public void onListItemClick(ListView l, View v, int position, long id) {    
  39.         showDetails(position);    
  40.     }    
  41.     
  42.     /**  
  43.      * Helper function to show the details of a selected item, either by  
  44.      * displaying a fragment in-place in the current UI, or starting a  
  45.      * whole new activity in which it is displayed.  
  46.      */    
  47.     void showDetails(int index) {    
  48.         mCurCheckPosition = index;    
  49.     
  50.         if (mDualPane) {    
  51.             // We can display everything in-place with fragments, so update    
  52.             // the list to highlight the selected item and show the data.    
  53.             getListView().setItemChecked(index, true);    
  54.     
  55.             // Check what fragment is currently shown, replace if needed.    
  56.             DetailsFragment details = (DetailsFragment)    
  57.                     getFragmentManager().findFragmentById(R.id.details);    
  58.             if (details == null || details.getShownIndex() != index) {    
  59.                 // Make new fragment to show this selection.    
  60.                 details = DetailsFragment.newInstance(index);    
  61.     
  62.                 // Execute a transaction, replacing any existing fragment    
  63.                 // with this one inside the frame.    
  64.                 FragmentTransaction ft = getFragmentManager().beginTransaction();    
  65.                 ft.replace(R.id.details, details);    
  66.                 ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);    
  67.                 ft.commit();    
  68.             }    
  69.     
  70.         } else {    
  71.             // Otherwise we need to launch a new activity to display    
  72.             // the dialog fragment with selected text.    
  73.             Intent intent = new Intent();    
  74.             intent.setClass(getActivity(), DetailsActivity.class);    
  75.             intent.putExtra("index", index);    
  76.             startActivity(intent);    
  77.         }    
  78.     }    
第二个 fragment DetailsFragment 显示被选择的 Title 的摘要:

[java]  view plain copy
  1. public static class DetailsFragment extends Fragment {    
  2.     /**  
  3.      * Create a new instance of DetailsFragment, initialized to  
  4.      * show the text at 'index'.  
  5.      */    
  6.     public static DetailsFragment newInstance(int index) {    
  7.         DetailsFragment f = new DetailsFragment();    
  8.     
  9.         // Supply index input as an argument.    
  10.         Bundle args = new Bundle();    
  11.         args.putInt("index", index);    
  12.         f.setArguments(args);    
  13.     
  14.         return f;    
  15.     }    
  16.     
  17.     public int getShownIndex() {    
  18.         return getArguments().getInt("index"0);    
  19.     }    
  20.     
  21.     @Override    
  22.     public View onCreateView(LayoutInflater inflater, ViewGroup container,    
  23.             Bundle savedInstanceState) {    
  24.         if (container == null) {    
  25.             // We have different layouts, and in one of them this    
  26.             // fragment's containing frame doesn't exist.  The fragment    
  27.             // may still be created from its saved state, but there is    
  28.             // no reason to try to create its view hierarchy because it    
  29.             // won't be displayed.  Note this is not needed -- we could    
  30.             // just run the code below, where we would create and return    
  31.             // the view hierarchy; it would just never be used.    
  32.             return null;    
  33.         }    
  34.     
  35.         ScrollView scroller = new ScrollView(getActivity());    
  36.         TextView text = new TextView(getActivity());    
  37.         int padding = (int)TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,    
  38.                 4, getActivity().getResources().getDisplayMetrics());    
  39.         text.setPadding(padding, padding, padding, padding);    
  40.         scroller.addView(text);    
  41.         text.setText(Shakespeare.DIALOGUE[getShownIndex()]);    
  42.         return scroller;    
  43.     }    
  44. }    
如果当前的 layout 没有 R.id.detailsView (它被用于 DetailsFragment 的容器),那么程序就启动 DetailsActivity 来显示摘要。

下面是DetailsActivity,它只是简单地嵌入DetailsFragment来显示摘要。

[java]  view plain copy
  1. public static class DetailsActivity extends Activity {    
  2.     
  3.     @Override    
  4.     protected void onCreate(Bundle savedInstanceState) {    
  5.         super.onCreate(savedInstanceState);    
  6.     
  7.         if (getResources().getConfiguration().orientation    
  8.                 == Configuration.ORIENTATION_LANDSCAPE) {    
  9.             // If the screen is now in landscape mode, we can show the    
  10.             // dialog in-line with the list so we don't need this activity.    
  11.             finish();    
  12.             return;    
  13.         }    
  14.     
  15.         if (savedInstanceState == null) {    
  16.             // During initial setup, plug in the details fragment.    
  17.             DetailsFragment details = new DetailsFragment();    
  18.             details.setArguments(getIntent().getExtras());    
  19.             getFragmentManager().beginTransaction().add(android.R.id.content, details).commit();    
  20.         }    
  21.     }    
  22. }    

注意这个activity在检测到是竖屏时会结束自己,于是主activity会接管它并显示出TitlesFragmentDetailsFragment。这可以在用户在竖屏时显示在TitleFragment,但用户旋转了屏幕,使显示变成了横屏。


以上部分内容转载或参考来源如下:
http://blog.csdn.net/t12x3456/article/details/8119568

http://blog.csdn.net/t12x3456/article/details/8119607

http://blog.csdn.net/t12x3456/article/details/8120309
在此表示感谢。
转载请注明来源,版权归原作者所有,未经同意严禁用于任何商业用途。
微博:http://weibo.com/theworldsong
邮箱:theworldsong@foxmail.com

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值