注:本文翻译自Google官方的Android Developers Training文档,译者技术一般,由于喜爱安卓而产生了翻译的念头,纯属个人兴趣爱好。

原文链接:http://developer.android.com/training/basics/fragments/communicating.html


为了重用Fragment UI组件,你应该将每一个组件构建为自控地,模块化的组件,它们有自己的布局和行为。一旦你定义好了这些可重用的fragment,你就可以将他们与activity关联,通过应用逻辑将它们连接起来,以此实现一个整体复合的界面。

你经常会希望一个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 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可以通过调用onArticleSelected()方法(或其他接口中的方法)向activity发送消息了。在此例中,使用的是OnHeadlineSelectedListener接口的实例:mCallback。

比如:下面所示的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);
}

二). 实现接口

为了接收来自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

宿主Activity可以通过findFragmentById()来获取fragment实例,之后就能调用fragment的公有方法,借此把消息传递给fragment。

举一个例子:我们想象上述的Activity可能还容纳了另一个fragment,它用来展示上述回调方法中岁返回特定项的数据。在这个例子中,activity可以把在回调方法中收到的信息发送给另一个fragment,这个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();
}
}
}

【Android Developers Training】 22. 与其他fragment通信的更多相关文章

  1. 【Android Developers Training】 20. 创建一个Fragment

    注:本文翻译自Google官方的Android Developers Training文档,译者技术一般,由于喜爱安卓而产生了翻译的念头,纯属个人兴趣爱好. 原文链接:http://developer ...

  2. 【Android Developers Training】 19. 序言:通过Fragments构建动态UI

    注:本文翻译自Google官方的Android Developers Training文档,译者技术一般,由于喜爱安卓而产生了翻译的念头,纯属个人兴趣爱好. 原文链接:http://developer ...

  3. 【Android Developers Training】 77. 使用Wi-Fi P2P进行服务搜索

    注:本文翻译自Google官方的Android Developers Training文档,译者技术一般,由于喜爱安卓而产生了翻译的念头,纯属个人兴趣爱好. 原文链接:http://developer ...

  4. 【Android Developers Training】 4. 启动另一个Activity

    注:本文翻译自Google官方的Android Developers Training文档,译者技术一般,由于喜爱安卓而产生了翻译的念头,纯属个人兴趣爱好. 原文链接:http://developer ...

  5. 【Android Developers Training】 107. 认知用户当前的行为

    注:本文翻译自Google官方的Android Developers Training文档,译者技术一般,由于喜爱安卓而产生了翻译的念头,纯属个人兴趣爱好. 原文链接:http://developer ...

  6. 【Android Developers Training】 106. 创建并检测地理围栏

    注:本文翻译自Google官方的Android Developers Training文档,译者技术一般,由于喜爱安卓而产生了翻译的念头,纯属个人兴趣爱好. 原文链接:http://developer ...

  7. 【Android Developers Training】 101. 显示快速联系人挂件(Quick Contact Badge)

    注:本文翻译自Google官方的Android Developers Training文档,译者技术一般,由于喜爱安卓而产生了翻译的念头,纯属个人兴趣爱好. 原文链接:http://developer ...

  8. 【Android Developers Training】 99. 获取联系人详细信息

    注:本文翻译自Google官方的Android Developers Training文档,译者技术一般,由于喜爱安卓而产生了翻译的念头,纯属个人兴趣爱好. 原文链接:http://developer ...

  9. 【Android Developers Training】 98. 获取联系人列表

    注:本文翻译自Google官方的Android Developers Training文档,译者技术一般,由于喜爱安卓而产生了翻译的念头,纯属个人兴趣爱好. 原文链接:http://developer ...

随机推荐

  1. XAF_GS_01_准备环境

    各位久等了,接下来我们开始学习XAF入门的第一节,搭建XAF的环境 Setp 1 第一步不是创建什么项目,而是先安装我们的XAF环境也就是安装DevExpress 由于笔者安装的是16.2.3所以演示 ...

  2. VR的技术问题是不是市场的绊脚石?

    VR虽然现在很火,但是不得不说,VR虚拟现实设备现在还没有普及,而且虚拟现实设备要想像手机一样普及,还面临着很多的困难和挑战.当然最重要的是,VR虚拟现实设备要解决一些问题才可以,这些问题也是影响VR ...

  3. 11、借助POI实现Java生成并打印excel报表(2)

    11.POI打印功能 11.1.常用模块形式: HSSFPrintSetup printSetup = sheet.getPrintSetup(); printSetup.setVResolution ...

  4. TableLayout

  5. TP框架 增删查

    TP框架添加数据到数据库1.使用数组方式添加造模型对象 2.使用AR方式 强类型语言存在的方式 3.使用自动收集表单添加 :只能用POST方式,提交数据一个操作方法实现两个逻辑:A显示页面B得到数据 ...

  6. 侯捷STL学习(一)

    开始跟着<STL源码剖析>的作者侯捷真人视频,学习STL,了解STL背后的真实故事! 视频链接:侯捷STL 还有很大其他视频需要的留言 第一节:STL版本和重要资源 STL和标准库的区别 ...

  7. Targeted Learning R Packages for Causal Inference and Machine Learning(转)

    Targeted learning methods build machine-learning-based estimators of parameters defined as features ...

  8. How-to go parallel in R – basics + tips(转)

    Today is a good day to start parallelizing your code. I’ve been using the parallel package since its ...

  9. vue-动手做个选择城市

    查看完整的代码请到   我的github地址  https://github.com/qianyinghuanmie/vue2.0-demos 一.结果展示 二.前期准备: 1.引入汉字转拼音的插件, ...

  10. OpenCv关于灰度积分图的SSE代码学习和改进。

    最近一直沉迷于SSE方面的优化,实在找不到想学习的参考资料了,就拿个笔记本放在腿上翻翻OpenCv的源代码,无意中看到了OpenCv中关于积分图的代码,仔细研习了一番,觉得OpenCv对SSE的灵活运 ...