关于谷歌官方对fragment交互数据的例子理解

来源:互联网 发布:linux公社 ftp 乱码 编辑:程序博客网 时间:2024/06/05 07:46

这个主要说一下fragment与activity之间的数据交互,以及两个fragment之间的交互,其实跟我之前工作中总结的fragment知识差不多,这里就再复习一下,

  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中声明一个回调接口,可以看到是上面的onHeadlineSelectedlisener(),由于fragemnt和activity的生命周期相关性,我们可以在attach()方法中进行回调对象接收,

  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中的方法中进行接口的回调,使用callback向activity提交数据,这样在activity中进行接口实现就可以拿到fragment中提交的数据

  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,可以这样做:

  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. }  
在回调方法中拿到数据,通过id查找到相关业务逻辑的fragment,如果没有在缓存中则会返回空,那么直接新创建一个,通过setarguments()这个方法将数据传递给fragment,如果在缓存中直接通过实例调用其内部的公共方法进行数据处理。

0 0