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

为了重用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
*
*/

 
 

Fragment间的通信的更多相关文章

  1. Android UI开发第二十六篇——Fragment间的通信

    为了重用Fragment的UI组件,创建的每个Fragment都应该是自包含的.有它自己的布局和行为的模块化组件.一旦你定义了这些可重用的Fragment,你就可以把它们跟一个Activity关联,并 ...

  2. Activity与Fragment之间的通信

    由于Fragment的生命周期完全依赖宿主Activity,所以当我们在使用Fragment时难免出现Activity和Fragment间的传值通信操作. 1.Activity向Fragment,通过 ...

  3. c 进程间的通信

    在上篇讲解了如何创建和调用进程 c 进程和系统调用 这篇文章就专门讲讲进程通信的问题 先来看一段下边的代码,这段代码的作用是根据关键字调用一个Python程序来检索RSS源,然后打开那个URL #in ...

  4. Directive间的通信

    Directive间的通信 源自大漠的<AngularJS>5个实例详解Directive(指令)机制 这个例子主要的难点在于如何在子Expander里面访问外层Accordion的sco ...

  5. Ucos系统任务间的通信详解

    物联网开发中,ucos系统任务间的通信是指,两个任务之间有数据的交互,具体的一起来看看吧. 1)消息邮箱 我们还是提供两个任务Task1和Task2,假设我们还是解决刚刚的问题,Task1进行按键扫描 ...

  6. Fragment之间的通信(四)

    自定义两个fragment的布局和java类. 在mainactivity中引用布局文件 在其中的一个fragment中的控件上添加监听,获取到另一个fragment中控件的内容,展示出来完成frag ...

  7. c# 进程间的通信实现之一简单字符串收发

       使用Windows API实现两个进程间(含窗体)的通信在Windows下的两个进程之间通信通常有多种实现方式,在.NET中,有如命名管道.消息队列.共享内存等实现方式,这篇文章要讲的是使用Wi ...

  8. Android进程间的通信之AIDL

    Android服务被设计用来执行很多操作,比如说,可以执行运行时间长的耗时操作,比较耗时的网络操作,甚至是在一个单独进程中的永不会结束的操作.实现这些操作之一是通过Android接口定义语言(AIDL ...

  9. Android进程间的通信之Messenger

    Android进程间的通信方式可以通过以下两种方式完成: Android接口定义语言(AIDL) 使用Messenger绑定服务 本文我们将学习使用Messenger绑定服务的方式进行进程间的通信. ...

随机推荐

  1. UVALive - 3942 Remember the Word[Trie DP]

    UVALive - 3942 Remember the Word Neal is very curious about combinatorial problems, and now here com ...

  2. Spring远程调用技术<1>-RMI

    在java中,我们有多种可以使用的远程调用技术 1.远程方法调用(remote method invocation, RMI)  适用场景:不考虑网络限制时(例如防火墙),访问/发布基于java的服务 ...

  3. String,StringBuffer,StringBuilder的区别

    public static void main(String[] args) { String str = new String("hello...."); StringBuffe ...

  4. 快速掌握iOS API的一个小技巧

    快速掌握iOS API的一个小技巧 周银辉 iOS SDK和Developer Library中提供了各个类以及函数的帮助文档,这很棒,但要想了解整个库的大体结构(比如UIKit下有哪些类,他们的继承 ...

  5. C#怎么遍历一个对象里面的全部属性?

    比如我现在有一个Student的对象,里面有属性stuName,stuAge,stuGender,我现在该怎么写循环才能遍历这几个属性? Student s=new...... foreach (Sy ...

  6. ubuntu系统升级记录

    之前在openstack中安装了ubuntu 12.04虚拟机,版本较低,需要升级为高版本.下面分享下升级过程: ubuntu系统升级操作:$ cat /etc/issueUbuntu 12.04.5 ...

  7. windows7配置Nginx+php+mysql教程

    windows7配置Nginx+php+mysql教程 最近在学习php,想把自己的学习经历记录下来,并写一些经验,仅供参考交流.此文适合那些刚刚接触php,想要学习并想要自己搭建Nginx+php+ ...

  8. Spring系列:学习Spring的资源和讨论

    1) 阅读<spring in action 4th edition>,这样可以对的spring可以做什么事情有个基本了解: 2) 阅读spring.io官网提供的各种reference, ...

  9. 转 Android RadioButton设置选中时文字和背景颜色同时改变

    主要应用在购物车,像淘宝的那样,点击以后弹出一个选择种类颜色这样的popuwindow以后,然后这个选择种类的地方要用到类似这个玩意儿. 搜了一下,效果和这个文章一致.转了. 原文地址:http:// ...

  10. Python2.7.6标准库内建函数

        Built-in Functions     abs() divmod() input() open() staticmethod() all() enumerate() int() ord( ...