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

为了重用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. java读取excel文件

    package com.execl; import java.io.File; import java.io.FileInputStream; import java.io.IOException; ...

  2. Python函数作用域的查找顺序

    函数作用域的LEGB顺序 1.什么是LEGB? L:local 函数内部作用域 E:enclosing 函数内部与内嵌函数之间 G:global 全局作用域 B:build-in 内置作用域 2.它们 ...

  3. 银行卡BIN: Bank Identification Number

    What is a 'Bank Identification Number - BIN'A bank identification number (BIN) is the initial four t ...

  4. bash 脚本编程 利用 “=” 赋值时,左右不能留空格

    对脚本变量用“=”赋值时, "=" 左右不能留有空格,否则会提示错误. 比如以下例子: #!/bin/bash BEGIN_TIME = `date +%H:%M:%S` ./a. ...

  5. JSON.net 在实体类中自定义日期的格式

    定义日期格式转换类,其继承 IsoDateTimeConverter,代码如下: public class DateTimeConverter : IsoDateTimeConverter { pub ...

  6. IE9下css hack写法

    ie9一出css hack也该更新,以前一直没关注,今天在内部参考群mxclion分享了IE9的css hack,拿出来也分享一下: select { background-color:red\0; ...

  7. 订餐系统之Excel批量导入

    批量导入现在基本已经成为各类系统的标配了,当前,我们订餐系统也不例外,什么商家呀.商品呀.优惠码之类的,都少不了.毕竟嘛,对非开发人员来说,看到Excel肯定比看到很多管理系统还是要亲切很多的.这里, ...

  8. 详解AsyncTask的使用

    转载自:http://blog.csdn.net/liuhe688/article/details/6532519 在Android中实现异步任务机制有两种方式,Handler和AsyncTask. ...

  9. BLE 蓝牙协议栈开发

    1.由浅入深,蓝牙4.0/BLE协议栈开发攻略大全(1) 2.由浅入深,蓝牙4.0/BLE协议栈开发攻略大全(2) 3.由浅入深,蓝牙4.0/BLE协议栈开发攻略大全(3)

  10. jsp利用application统计在线人数的方法

    <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"% ...