为了可以复用一个fragment,所以在定义fragment的时候,要把它定义为一个完全独立和模块化,它有它自己的layout和行为。当你定义好了这些可复用的fragment,可以把他们和activity相关联,在应用的逻辑基础上把这些fragment相互关联,从而组成一个完整的UI。

很多时候,我们需要fragment直接进行通信,比方说,根据用户的动作交换内容。所有的fragment直接的通信,都是利用与之关联的activity.2个fragment永远不可能直接通信。

定义接口

要允许fragment和它所在的activity通信,你可以在Fragment类里面定义一个接口,然后在activity里面实现这个接口。Fragment会在它的生命周期函数:onAttach()期间捕获这个接口的实现。然后就可以调用这个接口的方法和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就可以利用OnHeadlineSelectedListener这个接口的实例mCallback,来调用onArticleSelected()(或者是其他接口里面的方法)来给activity发送信息。

比方说,下面的例子就是,当用户点击一个list里面的一项的时候,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,必须实现在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.

 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创建动态UI 之 Fragment之间的通信的更多相关文章

  1. 9) 十分钟学会android--使用Fragment建立动态UI

    为了在 Android 上为用户提供动态的.多窗口的交互体验,需要将 UI 组件和 Activity 操作封装成模块进行使用,这样我们就可以在 Activity 中对这些模块进行切入切出操作.可以用  ...

  2. 【ASP.NET Web API教程】2.3.5 用Knockout.js创建动态UI

    原文:[ASP.NET Web API教程]2.3.5 用Knockout.js创建动态UI 注:本文是[ASP.NET Web API系列教程]的一部分,如果您是第一次看本博客文章,请先看前面的内容 ...

  3. Android学习路径(23)应用Fragment建立动态UI——Fragment之间的通信

    为了要重用Fragment的UI组件.你应该为它们每个都构建一个完整独立的,模块化的组件来定义他自身的布局和行为. 一旦你定义了这些可重用的Fragments.你能够通过activity关联它们同一时 ...

  4. 【译】用Fragment创建动态的界面布局(附Android示例代码)

    原文链接:Building a Dynamic UI with Fragments 为了在Android上创建一个动态和多视图的用户界面,你需要封装UI控件和模块化Activity的行为,以便于你能够 ...

  5. Android学习路线(二十)运用Fragment构建动态UI

    要在Android系统上创建一个动态或者多面板的用户界面,你须要将UI组件以及activity行为封装成模块.让它可以在你的activity中灵活地切换显示与隐藏. 你可以使用Fragment类来创建 ...

  6. Android学习路径(22)应用Fragment建立动态UI——构建一个灵活UI

    当你设计你的应用来支持多个屏幕尺寸.你能够基于可用的屏幕空间通过在不同的布局上重用fragment来优化用户体验. 比如,在一个手机上.使用单面板(一次仅仅显示一个fragment)的用户体验更加合适 ...

  7. 使用Fragment 实现动态UI 和 动态添加Fragment

    首先写好每个Fragment: 1.在第一个Fragment写一个按钮,使其加载下一个Fragment 布局: <LinearLayout xmlns:android="http:// ...

  8. Fragment的生命周期和Activity之间的通信以及使用

    Fragment通俗来讲就是碎片,不能单独存在,意思就是说必须依附于Activity,一般来说有两种方式把Fragment加到Activity,分为静态,动态. 静态即为右键单击,建立一个Fragme ...

  9. Android学习路线(二十一)运用Fragment构建动态UI——创建一个Fragment

    你能够把fragment看成是activity的模块化部分.它拥有自己的生命周期,接受它自己的输入事件,你能够在activity执行时加入或者删除它(有点像是一个"子activity&quo ...

随机推荐

  1. 关于Aazure 使用以前保留的vhd创建虚拟机的基本步骤

    1. 删除vm保留vhd(只删除虚拟机记录,不删除磁盘)2. 拷贝vhd以及status文件到指定的存储账号3. 使用拷贝的VHD创建disk4. 从disk创建vm,指定指定vnet以及cloud ...

  2. memcached全面剖析–3. memcached的删除机制和发展方向

    memcached在数据删除方面有效利用资源 数据不会真正从memcached中消失 上次介绍过, memcached不会释放已分配的内存.记录超时后,客户端就无法再看见该记录(invisible,透 ...

  3. Unity3D之Mecanim动画系统学习笔记(五):Animator Controller

    简介 Animator Controller在Unity中是作为一种单独的配置文件存在的文件类型,其后缀为controller,Animator Controller包含了以下几种功能: 可以对多个动 ...

  4. MVC3 使用NPOI导出excel

    NPOI的官方教程:http://tonyqus.sinaapp.com/tutorial 此次项目中使用的是NPOI的1.25版本,项目是MVC3,在这里只贴上controller部分的代码: 1 ...

  5. SQLite使用事务更新—by command

    public void SaveToDB(DataTable dt) { /* todo:sqlite没有提供批量插入的机制,需要通过事务处理 更新所有数据 * http://www.cnblogs. ...

  6. Java二维码登录流程实现(包含短地址生成,含部分代码)

    近年来,二维码的使用越来越风生水起,笔者最近手头也遇到了一个需要使用二维码扫码登录网站的活,所以研究了一下这一套机制,并用代码实现了整个流程,接下来就和大家聊聊二维码登录及的那些事儿. 二维码原理 二 ...

  7. Flex友好提示、警告

    Flex 自带的Alert带给用户的体验并不好,对于一些简单的提示来说,这个小题大作了. <?xml version="1.0" encoding="utf-8&q ...

  8. Chrome插件开发 尝试1

    1.新建文件夹 如图:整个项目的结构 2.新建一个名为manifest.json的文件,编码模式为utf-8,(可以先建好txt文件然后再将文件后缀txt改为json) 3.用记事本写入代码如下:(m ...

  9. Caching in ASP.NET MVC

    The caching options available in ASP.NET MVC applications don’t come from the ASP.NET MVC Framework, ...

  10. C# 制作外挂常用的API

    C#做外挂的常用API,本人用了很久,基本没发现问题 using System; using System.Collections.Generic; using System.Text; using  ...