原文地址:http://android.xsoftlab.net/training/basics/fragments/communicating.html

为了可以重复使用Fragment UI组件,你应该将fragment构建为一个完整的独立的模块化组件,并且它可以定义自己的布局和行为习惯。你只要定义了一次这类可复用的fragment,你就可以通过activity与之相关联,然后使用应用程序逻辑与之建立连接以实现完整的复合型UI组件。

经常你会希望一个fragment对象可以和其它的fragment对象进行通信,举个例子,基于用户的事件更改内容。所有的fragment之间的通信都是通过它们关联的activity进行的。两个fragment对象应该永远不要直接通信。

定义一个接口

为了使fragment与其关联的activity建立通信渠道,你可以在fragment中定义一个接口,然后让activity实现它。这个fragment对象可以在生命周期方法onAttach中取得这个接口的实现,然后就可以调用这个接口的方法与其关联的activity进行通信了。

下面是fragment与activity通信的例子:

public class HeadlinesFragment extends ListFragment {OnHeadlineSelectedListener mCallback;// Container Activity must implement this interfacepublic interface OnHeadlineSelectedListener {public void onArticleSelected(int position);}@Overridepublic void onAttach(Activity activity) {super.onAttach(activity);// This makes sure that the container activity has implemented// the callback interface. If not, it throws an exceptiontry {mCallback = (OnHeadlineSelectedListener) activity;} catch (ClassCastException e) {throw new ClassCastException(activity.toString()+ " must implement OnHeadlineSelectedListener");}}...
}

现在fragment就可以通过调用OnHeadlineSelectedListener接口的具体实现对象mCallback的onArticleSelected方法(或者接口中的其它方法)给activity传送消息了。

举个例子,下面这个方法会在用户点击列表条目的时候被调用,然后fragment会使用回调接口将事件传送给它所附属的activity。

实现接口

为了可以从fragment中接收事件回调,那么包含fragment的activity必须实现在fragment中定义的接口。

举个例子,下面是上面例子中的在activity中的接口实现:

public static class MainActivity extends Activityimplements HeadlinesFragment.OnHeadlineSelectedListener{...public void onArticleSelected(int position) {// The user selected the headline of an article from the HeadlinesFragment// Do something here to display that article}
}

发送消息给fragment

宿主activity可以通过findFragmentById()方法获得fragment的对象,然后再发送消息到fragment中。可以直接调用fragment的public方法发送消息。

举个例子,想象上面例子中的activity可能还包含有其它的fragment,这些fragment可能被用来显示上面回调方法返回的数据。在这个例子中,activity会将从回调方法中接收到的信息传递给其它的fragment,其它的fragment会将这些信息显示出来:

public static class MainActivity extends Activityimplements HeadlinesFragment.OnHeadlineSelectedListener{...public void onArticleSelected(int position) {// The user selected the headline of an article from the HeadlinesFragment// Do something here to display that articleArticleFragment articleFrag = (ArticleFragment)getSupportFragmentManager().findFragmentById(R.id.article_fragment);if (articleFrag != null) {// If article frag is available, we're in two-pane layout...// Call a method in the ArticleFragment to update its contentarticleFrag.updateArticleView(position);} else {// Otherwise, we're in the one-pane layout and must swap frags...// Create fragment and give it an argument for the selected articleArticleFragment newFragment = new ArticleFragment();Bundle args = new Bundle();args.putInt(ArticleFragment.ARG_POSITION, position);newFragment.setArguments(args);FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();// Replace whatever is in the fragment_container view with this fragment,// and add the transaction to the back stack so the user can navigate backtransaction.replace(R.id.fragment_container, newFragment);transaction.addToBackStack(null);// Commit the transactiontransaction.commit();}}
}

Android官方开发文档Training系列课程中文版:使用Fragment构建动态UI之与其它Fragment通信相关推荐

  1. Android官方开发文档Training系列课程中文版:目录

    原文地址 : http://android.xsoftlab.net/training/index.html 引言 在翻译了一篇安卓的官方文档之后,我觉得应该做一件事情,就是把安卓的整篇训练课程全部翻 ...

  2. Android官方开发文档Training系列课程中文版:创建自定义View之View的创建

    原文地址:http://android.xsoftlab.net/training/custom-views/index.html 引言 Android框架含有大量的View类,这些类用来显示各式各样 ...

  3. Android官方开发文档Training系列课程中文版:OpenGL绘图之图形绘制

    原文地址:http://android.xsoftlab.net/training/graphics/opengl/draw.html 如果你还不清楚如何定义图形及坐标系统,请移步:Android官方 ...

  4. Android官方开发文档Training系列课程中文版:使用Fragment构建动态UI之Fragment创建

    原文地址:http://android.xsoftlab.net/training/basics/fragments/index.html 导言 为了在Android中创建动态的多面板用户界面,你需要 ...

  5. Android官方开发文档Training系列课程中文版:构建第一款安卓应用之入门指南

    入门指南 欢迎来到安卓开发训练课,在这里你可以找到一系列课程来描述如何使用现有的代码示例来重新适用到你的APP上,你可以在左侧的导航栏顶部看到在若干个大项里有若干个有组织的子项课程.(导航栏请参见官方 ...

  6. Android官方开发文档Training系列课程中文版:键盘输入处理之控制输入法的显示方式

    原文地址:http://android.xsoftlab.net/training/keyboard-input/visibility.html 当输入的焦点进入或者离开文本框时,Android会适时 ...

  7. Android官方开发文档Training系列课程中文版:打印内容之图像打印

    原文地址:http://android.xsoftlab.net/training/printing/index.html 引言 Android用户会很频繁的浏览设备上的内容,但是有部分情况例外,当屏 ...

  8. Android官方开发文档Training系列课程中文版:分享文件之配置文件共享

    原文地址:http://android.xsoftlab.net/training/secure-file-sharing/index.html 导言 APP经常需要给其它的APP提供一个或多个文件. ...

  9. Android官方开发文档Training系列课程中文版:添加ActionBar之设置ActionBar

    导言- 添加ActionBar 原文地址:http://android.xsoftlab.net/training/basics/actionbar/index.html ActionBar是很多重要 ...

最新文章

  1. 软件工程综合实践阶段小结(2)
  2. Ogre 编辑器二(用Ogre的地形组件加载天龙八部地形)
  3. 计算机视觉与深度学习 | TensorMask: A Foundation for Dense Object Segmentation(何凯明团队新作)近5年目标检测综述
  4. ibm 小型计算机8408,IBM小型机 Power8 E850配置好不好
  5. sap 发送mesage_SAP的message机制
  6. Java 8中Lambda表达式的阴暗面
  7. 开发优秀产品的六大秘诀
  8. InnoDB文件系统
  9. js计算两个整数之间的百分比
  10. 小白都能看得懂的java回调
  11. matlab中的常用符号,matlab特殊符号表
  12. 《SysML精粹》学习记录--第七章
  13. 编码中关于二义性的解释
  14. Spring IOC理论推导及其本质
  15. 闲聊如何成为产品经理
  16. Linux查看主机信息及修改主机名hostname
  17. 数据库设计之冗余、索引以及查询优化
  18. 计算机话筒技术指标,手把手教你搞懂麦克风的技术指标
  19. java的tey语句return了_Java中try、finally语句中有return时的执行情况
  20. SAS 运行过程中出现‘SAS 磁盘已满 OUT OF RESOURCES’解决办法!

热门文章

  1. boost.asio防止恶意空连接的方法
  2. 随便聊聊,Linux 中的环境变量
  3. 轻轻的你来了,悄悄的你走了,邓总没有带走一个bug
  4. Mtk camera driver
  5. java互换_两个变量交换的四种方法(Java)
  6. colinux的安装
  7. JS高级——Proxy、Reflect
  8. 十九、PHP框架Laravel学习笔记——批量赋值和软删除
  9. 三、Java Web中出现的一些乱码问题总结(详解)
  10. LeetCode 2064. 分配给商店的最多商品的最小值(二分查找)