Android之Fragment通信

来源:互联网 发布:mac adobe设置中文版 编辑:程序博客网 时间:2024/04/28 17:37

为了重用Fragment的UI组件,创建的每个Fragment都应该是自包含的、有它自己的布局和行为的模块化组件。一旦你定义了这些可重用的Fragment,你就可以把它们跟一个Activity关联,并把它们跟应用程序的逻辑相连来实现全部的组合式UI。

       现实中我们经常想要一个Fragment跟另一个Fragment进行通信,例如,要基于一个用户事件来改变内容。所有的Fragment间的通信都是通过跟关联的Activity来完成的。另个Fragment不应该直接通信。也就是说Fragment间不直接通信,通过Activity转一下,按java常规,转一下多是使用Interface实现的。

定义Interface

       为了让Fragment跟它的Activity通信,你可以在Fragment类中定义一个接口,并在它所属的Activity中实现该接口。Fragment在它的onAttach()方法执行期间捕获该接口的实现,然后就可以调用接口方法,以便跟Activity通信。

以下是Fragment跟Activity通信的示例:

[java] view plaincopyprint?
  1. public class HeadlinesFragment extends ListFragment {  
  2.     OnHeadlineSelectedListener mCallback;  
  3.   
  4.     // Container Activity must implement this interface  
  5.     public interface OnHeadlineSelectedListener {  
  6.         public void onArticleSelected(int position);  
  7.     }  
  8.   
  9.     @Override  
  10.     public void onAttach(Activity activity) {  
  11.         super.onAttach(activity);  
  12.           
  13.         // This makes sure that the container activity has implemented  
  14.         // the callback interface. If not, it throws an exception  
  15.         try {  
  16.             mCallback = (OnHeadlineSelectedListener) activity;  
  17.         } catch (ClassCastException e) {  
  18.             throw new ClassCastException(activity.toString()  
  19.                     + " must implement OnHeadlineSelectedListener");  
  20.         }  
  21.     }  
  22.       
  23.     ...  
  24. }  

      现在,这个Fragment就可以通过调用OnHealdlineSelectedListener接口实例mCallback的onArticleSelected()方法(或其他的接口中的方法)给Activity发送消息。

例如,在Fragment中的下列方法会用户点击列表项时被调用。该Fragment使用回调接口把该事件发送给它的父Activity。

[java] view plaincopyprint?
  1. @Override  
  2.    public void onListItemClick(ListView l, View v, int position, long id) {  
  3.        // Send the event to the host activity  
  4.        mCallback.onArticleSelected(position);  
  5.    }  


实现Interface

为了从Fragment中接收事件回调,包含Fragment的Activity必须实现Fragment类中定义的接口。

例如,下面Activity实现了上面示例中定义的接口:

[java] view plaincopyprint?
  1. public static class MainActivity extends Activity  
  2.         implements HeadlinesFragment.OnHeadlineSelectedListener{  
  3.     ...  
  4.       
  5.     public void onArticleSelected(int position) {  
  6.         // The user selected the headline of an article from the HeadlinesFragment  
  7.         // Do something here to display that article  
  8.     }  
  9. }  



把消息传递给另一个Fragment

         通过使用findFragmentById()方法捕获Fragment实例,宿主Activity可以把消息发送给该Fragment,然后直接调用该Fragment的公共方法。

        例如,上面的示例,Activty通过Interface的实现方法,传递数据到另一个Fragment。

[java] view plaincopyprint?
  1. public static class MainActivity extends Activity  
  2.         implements HeadlinesFragment.OnHeadlineSelectedListener{  
  3.     ...  
  4.   
  5.     public void onArticleSelected(int position) {  
  6.         // The user selected the headline of an article from the HeadlinesFragment  
  7.         // Do something here to display that article  
  8.   
  9.         ArticleFragment articleFrag = (ArticleFragment)  
  10.                 getSupportFragmentManager().findFragmentById(R.id.article_fragment);  
  11.   
  12.         if (articleFrag != null) {  
  13.             // If article frag is available, we're in two-pane layout...  
  14.   
  15.             // Call a method in the ArticleFragment to update its content  
  16.             articleFrag.updateArticleView(position);  
  17.         } else {  
  18.             // Otherwise, we're in the one-pane layout and must swap frags...  
  19.   
  20.             // Create fragment and give it an argument for the selected article  
  21.             ArticleFragment newFragment = new ArticleFragment();  
  22.             Bundle args = new Bundle();  
  23.             args.putInt(ArticleFragment.ARG_POSITION, position);  
  24.             newFragment.setArguments(args);  
  25.           
  26.             FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();  
  27.   
  28.             // Replace whatever is in the fragment_container view with this fragment,  
  29.             // and add the transaction to the back stack so the user can navigate back  
  30.             transaction.replace(R.id.fragment_container, newFragment);  
  31.             transaction.addToBackStack(null);  
  32.   
  33.             // Commit the transaction  
  34.             transaction.commit();  
  35.         }  
  36.     }  
  37. }  


Fragment中使用左右滑动菜单 中应用到了Fragment间的通信


参考:http://developer.android.com/training/basics/fragments/communicating.html

0 0
原创粉丝点击