在网上看到的一篇文章,总结的很好

为了重用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通信的示例:

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

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

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

  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实现了上面示例中定义的接口:

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

把消息传递给另一个Fragment

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

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

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

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

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

/**
* @author 张兴业
* 邮箱:xy-zhang#163.com
* android开发进阶群:241395671
*
*/

 
 

最新文章

  1. ubuntu安装配置elasticSearch(vagrant)
  2. HTML标签之<q> <blockquote>
  3. 判断 0 和 '' 以及 empty null false的关系
  4. C#常用集合的使用(转载)
  5. SCU 4424(求子集排列数)
  6. linux epoll 学习
  7. 使用openssl工具生成证书
  8. web开发下的各种下载方法
  9. Oracle 数据泵导入导出总结
  10. HUD --- 3635
  11. [转]机器学习&数据挖掘笔记_16(常见面试之机器学习算法思想简单梳理)
  12. HTML之标签
  13. ElasticSearch(7)-排序
  14. Java 后端微信小程序支付demo (网上说的坑里面基本上都有)
  15. [转]Blue Prism Architecture
  16. 产品经理说|AIOps 让告警管理变得更智能
  17. SIMTRACE环境搭建
  18. eclipse引入php源包
  19. sql server全文索引使用中的小坑 (转载)
  20. Tag Helpers in forms in ASP.NET Core

热门文章

  1. jdbc java数据库连接 1)jdbc入门
  2. Metasploit爆破tcpwrapped服务
  3. codevs 2894 保留小数
  4. 【转】【C#】迭代器
  5. edge 浏览器中数字显示为链接
  6. 树莓派Odroid等卡片式电脑上搭建NAS教程系列6-miniDLNA
  7. 20145208《信息安全系统设计基础》实验五 简单嵌入式WEB 服务器实验
  8. time & datetime
  9. 如何完全卸载(Mac&Windows)office 365 ProPlus
  10. RAM、DRAM、SD卡