为了要重用Fragment的UI组件。你应该为它们每个都构建一个完整独立的,模块化的组件来定义他自身的布局和行为。

一旦你定义了这些可重用的Fragments。你能够通过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 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传递消息了。

比如。当用户点击fragment中列表的item时,以下的方法将会被调用。这个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的回调事件,包括这个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的public方法。

比如,设想上面的那个activity包括还有一个fragment,这个fragment是用来展示通过前面的回调方法返回的数据指定的item。在这个案例中,这个activity能够这个activity能够将接收的这些信息传递给其他的fragment用来显示这个item:

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学习路径(23)应用Fragment建立动态UI——Fragment之间的通信的更多相关文章

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

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

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

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

  3. Android学习路径(七)建立Action Bar

    在action bar最今本的形式中,它只在左边展示了activity的标题以及应用的icon. 即使在这样的简单的形式中,它也不过告诉用户如今在应用的哪个activity中,同一时候为你的应用保持一 ...

  4. Android学习路线(二十四)ActionBar Fragment运用最佳实践

    转载请注明出处:http://blog.csdn.net/sweetvvck/article/details/38645297 通过前面的几篇博客.大家看到了Google是怎样解释action bar ...

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

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

  6. Android学习系列(23)--App主界面实现

    在上篇文章<Android学习系列(22)--App主界面比较>中我们浅略的分析了几个主界面布局,选了一个最大众化的经典布局.今天我们就这个经典布局,用代码具体的实现它. 1.预览图先看下 ...

  7. 我的android学习经历23

    学习fragment时遇到的问题 这几天学习fragment静态加载时遇到这样的问题: java.lang.RuntimeException: Unable to start activity Com ...

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

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

  9. Android学习笔记(九)一个例子弄清Service与Activity通信

    上一篇博文主要整理了Service的创建.绑定过程,本篇主要整理一下Service与Activity的通信方式.包括在启动一个Service时向它传递数据.怎样改变运行中的Service中得数据和侦听 ...

随机推荐

  1. 【web开发学习笔记】Structs2 Action学习笔记(一个)

    1.org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter准备和运行 2. <filter-mapping&g ...

  2. android: WheelView组件(滑轮组件)的应用!

    android前段组件中, 填表单,选择条目 的样式有很多, WheelView滚动组件为其中一种,如下图所示:                                          前两 ...

  3. Apache Lucene

    1.Lucene  -全文搜索引擎 Apache Lucene 是一个基于Java的全文搜索引擎,利用它能够轻易的为Java软件添�全文搜索引擎的功能. Lucene最重要的工作是替文件的每个字索引, ...

  4. Swift 编程语言新手教程

    今天在网上看到一篇很好的教程,分享给大家 原文地址:http://gashero.iteye.com/blog/2075324 文件夹 1   简单介绍 2   Swift入门 3   简单值 4   ...

  5. AdaBoost中利用Haar特征进行人脸识别算法分析与总结1——Haar特征与积分图

    原地址:http://blog.csdn.net/watkinsong/article/details/7631241 目前因为做人脸识别的一个小项目,用到了AdaBoost的人脸识别算法,因为在网上 ...

  6. 在Ubuntu上录制视频和编辑(很全)

    Linux多媒体三剑客:GIMP,Inkscape,Blender3D Blender基金会制作的开源微电影Sintel:http://www.sintel.org/about电影采用Creative ...

  7. linux find命令强大之处

    find命令 find pathname -options [-print -exec -ok ...]   -print: find命令将匹配的文件输出到标准输出.   -exec: find命令对 ...

  8. oracle 之 内存—鞭辟近里(四)

    oracle 之 内存—鞭辟近里(四) 今天是2013-07-11日,首先我非常感谢我的哥们也是我的网友杨工,非常感谢他能在大数据库内帮我执行一下我所需要的信息.就是他说的网络真是一个互助友爱的平台. ...

  9. NET Core 1.0 RC2

    NET Core 1.0 RC2 历险之旅 文章背景:对于.NET Core大家应该并不陌生, 从它被 宣布 到现在已经有1-2年的时间了,其比较重要的一个版本1.0 RC2 也即将发布..Net C ...

  10. 解决windows下的mysql匿名登陆无法使用mysql数据库的问题

    原文:解决windows下的mysql匿名登陆无法使用mysql数据库的问题 我在windows下安装了mysql,但是不用密码就能登进去,而root明明是有密码的,我用select user()命令 ...