Android Developers:和其它Fragment通信

为了重用Fragment UI组件,你应该作为一个定义了它自己的布局和行为的,完全独立的,模块化的组建来构建。一旦你定义了这些可重用的Fragment,你使用一个Activity关联它们,和结合应用程序的逻辑以实现整体复合界面。 

 

经常你会想让一个fragment和另一个通信,例如基于用户事件改变内容。所有Fragment和Fragment的通信是通过相关的Activity完成。两个Fragment不能直接通信。 

 

定义一个接口 

————————————————————————————————————————————————————————————— 

为了允许fragment和它的activity通信,你可以在这个Fragment类中定义一个接口,并在这个Activity中实现它。这个fragment在它的onAttach()生命周期方法中获取接口的实现,然后调用这个接口方法和activity通信。 

 

[java]  view plain copy
 
  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能使用onHeadlineSelectedListener接口的mCallback实例,调用onArticleSelected()方法(或者在接口中的其它方法)发送消息给activity。 

 

例如,下面在fragment中的方法当用户点击列表项的时候被调用。fragment使用回调接口来想父activity发送这个事件。 

 

[java]  view plain copy
 
  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.     }   

 

实现接口 

———————————————————————————————————————————————— 

为了从fragment获取事件回调方法,寄存它的activity必须实现被定义在fragment类中的接口。 

 

例如,下面的activity实现了上面例子的接口: 

 

[java]  view plain copy
 
  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发送消息 

————————————————————————————————————————— 

寄主activity能向使用findFragmentById()方法捕获的Fragment实例发送消息,然后直接调用fragment的公共方法。 

 

例如,想象一下,上面显示的activity可能包含另一个fragment,它被用于显示选项详情,通过上面的回调方法被返回的数据。在这种情况下,activity能想另一个显示选项的fragment,传递在回调方法中获取的信息。 

 

[java]  view plain copy
 
  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. }   

猜你喜欢

转载自252190908.iteye.com/blog/1964298