Fragment间的通信
在网上看到的一篇文章,总结的很好
为了重用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通信的示例:
- public class HeadlinesFragment extends ListFragment {
- OnHeadlineSelectedListener mCallback;
- // Container Activity must implement this interface
- public interface OnHeadlineSelectedListener {
- public void onArticleSelected(int position);
- }
- @Override
- public void onAttach(Activity activity) {
- super.onAttach(activity);
- // This makes sure that the container activity has implemented
- // the callback interface. If not, it throws an exception
- try {
- mCallback = (OnHeadlineSelectedListener) activity;
- } catch (ClassCastException e) {
- throw new ClassCastException(activity.toString()
- + " must implement OnHeadlineSelectedListener");
- }
- }
- ...
- }
现在,这个Fragment就可以通过调用OnHealdlineSelectedListener接口实例mCallback的onArticleSelected()方法(或其他的接口中的方法)给Activity发送消息。
例如,在Fragment中的下列方法会用户点击列表项时被调用。该Fragment使用回调接口把该事件发送给它的父Activity。
- @Override
- public void onListItemClick(ListView l, View v, int position, long id) {
- // Send the event to the host activity
- mCallback.onArticleSelected(position);
- }
实现Interface
为了从Fragment中接收事件回调,包含Fragment的Activity必须实现Fragment类中定义的接口。
例如,下面Activity实现了上面示例中定义的接口:
- public static class MainActivity extends Activity
- implements 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
通过使用findFragmentById()方法捕获Fragment实例,宿主Activity可以把消息发送给该Fragment,然后直接调用该Fragment的公共方法。
例如,上面的示例,Activty通过Interface的实现方法,传递数据到另一个Fragment。
- public static class MainActivity extends Activity
- implements 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
- ArticleFragment 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 content
- articleFrag.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 article
- ArticleFragment 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 back
- transaction.replace(R.id.fragment_container, newFragment);
- transaction.addToBackStack(null);
- // Commit the transaction
- transaction.commit();
- }
- }
- }
Fragment中使用左右滑动菜单 中应用到了Fragment间的通信
参考:http://developer.android.com/training/basics/fragments/communicating.html
/**
* @author 张兴业
* 邮箱:xy-zhang#163.com
* android开发进阶群:241395671
*
*/
Fragment间的通信的更多相关文章
- Android UI开发第二十六篇——Fragment间的通信
为了重用Fragment的UI组件,创建的每个Fragment都应该是自包含的.有它自己的布局和行为的模块化组件.一旦你定义了这些可重用的Fragment,你就可以把它们跟一个Activity关联,并 ...
- Activity与Fragment之间的通信
由于Fragment的生命周期完全依赖宿主Activity,所以当我们在使用Fragment时难免出现Activity和Fragment间的传值通信操作. 1.Activity向Fragment,通过 ...
- c 进程间的通信
在上篇讲解了如何创建和调用进程 c 进程和系统调用 这篇文章就专门讲讲进程通信的问题 先来看一段下边的代码,这段代码的作用是根据关键字调用一个Python程序来检索RSS源,然后打开那个URL #in ...
- Directive间的通信
Directive间的通信 源自大漠的<AngularJS>5个实例详解Directive(指令)机制 这个例子主要的难点在于如何在子Expander里面访问外层Accordion的sco ...
- Ucos系统任务间的通信详解
物联网开发中,ucos系统任务间的通信是指,两个任务之间有数据的交互,具体的一起来看看吧. 1)消息邮箱 我们还是提供两个任务Task1和Task2,假设我们还是解决刚刚的问题,Task1进行按键扫描 ...
- Fragment之间的通信(四)
自定义两个fragment的布局和java类. 在mainactivity中引用布局文件 在其中的一个fragment中的控件上添加监听,获取到另一个fragment中控件的内容,展示出来完成frag ...
- c# 进程间的通信实现之一简单字符串收发
使用Windows API实现两个进程间(含窗体)的通信在Windows下的两个进程之间通信通常有多种实现方式,在.NET中,有如命名管道.消息队列.共享内存等实现方式,这篇文章要讲的是使用Wi ...
- Android进程间的通信之AIDL
Android服务被设计用来执行很多操作,比如说,可以执行运行时间长的耗时操作,比较耗时的网络操作,甚至是在一个单独进程中的永不会结束的操作.实现这些操作之一是通过Android接口定义语言(AIDL ...
- Android进程间的通信之Messenger
Android进程间的通信方式可以通过以下两种方式完成: Android接口定义语言(AIDL) 使用Messenger绑定服务 本文我们将学习使用Messenger绑定服务的方式进行进程间的通信. ...
随机推荐
- UVA - 1025 A Spy in the Metro[DP DAG]
UVA - 1025 A Spy in the Metro Secret agent Maria was sent to Algorithms City to carry out an especia ...
- 微信接口-获取用户openid基本信息
一.协助获取微信用户openid功能 https://open.weixin.qq.com/connect/oauth2/authorize?appid=APPID&redirect_uri= ...
- jquery的各种隐藏显现动画的区别
<!DOCTYPE html> <html> <head lang="en"> <meta charset="utf-8&quo ...
- 安装node-sass提示没有vendor目录的解决办法
在node-sass目录下面新建一个vendor的空目录,然后运行npm/cnpm rebuild node-sass --save-dev即可,如果安装失败,会生成一个目录名为类似这样win32-x ...
- 简单了解ICMP协议
ping命令是什么协议? 维基百科: ping是一种电脑网络工具,用来测试数据包能否通过IP协议到达特定主机.ping的运作原理是向目标主机传出一个ICMP echo@要求数据包,并等待接受echo回 ...
- Oracle系列——开发中奇葩问题你遇到几个(一)
前言:在使用oracle数据进行开发的时候有没有经常出现一些很奇怪.很纳闷.很无厘头的问题呢.下面是本人使用oracle一段时间遇到的问题小节,在此做个记录,方便以后再遇到类似的问题能快速解决.如果你 ...
- ionic配置 问题小记
1.用命令ionic start myApp tabs新建项目时,在最后面提示ionic\cli.js报错的问题(具体问题描述忘记了) 安装 node-inspector 即可 ,使用命令 cnpm ...
- oracle误删表解决方案
·delete(删除一条记录)·drop或truncate删除表格中数据 1.delete误删除的解决方法原理:利用oracle提供的闪回方法,如果在删除数据后还没做大量的操作(只要保证被删除数据的块 ...
- hihoCoder 后缀自动机三·重复旋律6
后缀自动机三·重复旋律6 时间限制:15000ms 单点时限:3000ms 内存限制:512MB 描述 小Hi平时的一大兴趣爱好就是演奏钢琴.我们知道一个音乐旋律被表示为一段数构成的数列. 现在小Hi ...
- java连接mysql
Java 连接 MySQL 需要驱动包,最新版下载地址为:http://dev.mysql.com/downloads/connector/j/,解压后得到jar库文件,然后在对应的项目中导入该库文件 ...