Android Fragment---给Activity创建事件回调
2012-04-26 01:12
459 查看
在某些案例中,可能需要Fragment与Activity共享事件。在Fragment内部定义一个回调接口是一个好方法,并且规定由持有它的Activity实现这个回调方法。当Activity通过接口接受回调时,它能在必要时与布局中的其他Fragment共享信息。
例如,如果一个新闻类的应用程序在一个Activity中有两个Fragment---一个用来显示文章列表(Fragment A),另一个用来显示文章内容(Fragment B)---然后再列表项目被选中时Fragment A必须告诉Activity,以便它能告诉Fragment B显示对应的文章。在下面的例子中在Fragment A的内部声明了OnArticleSelectedListener接口。
然后,持有这个Fragment的Activity要实现OnArticleSelectedListener接口,并且要重写onArticleSelected()方法把来自Fragment A的事件通知给Fragment B。要确保持有Fragment的Activity实现这个接口, Fragment A 的onAttach()回调方法(当Fragment被添加到Activity时系统调用这个方法)通过类型转换onAttach()传入的Activity来实例化一个OnArticleSelectedListener的实例。
如果这个Activity没有实现这个接口,那么Fragment会抛出ClassCastException异常。如果成功,那么mListener成员就会拥有Activity实现的OnArticleSelectedListener对象的引用,以便Fragment A能够通过OnArticleSelectedListener接口定义的回调方法和Activity共享事件。例如,如果ListFragment继承了Fragment A,那么用户每次点击列表项时,系统都会调用Fragment中的onListItemClick()方法,然后调用onArticleSelected()方法和Activity共享事件:
传递给onListItemClick()的id参数是被点击项目的行ID,Activity(或其他的Fragment)使用这个ID从应用程序的ContentProvider对象中获取对应的文章。
关于使用有效内容提供器的更多内容,请看内容提供器(Content Providers)文档。
注:本人转载系个人觉得翻译的很好,值得收藏,且自己回头看着方便。
如有兴趣请访问作者官方博客http://blog.csdn.net/FireOfStar
例如,如果一个新闻类的应用程序在一个Activity中有两个Fragment---一个用来显示文章列表(Fragment A),另一个用来显示文章内容(Fragment B)---然后再列表项目被选中时Fragment A必须告诉Activity,以便它能告诉Fragment B显示对应的文章。在下面的例子中在Fragment A的内部声明了OnArticleSelectedListener接口。
public static class FragmentA extends ListFragment { ... // Container Activity must implement this interface public interface OnArticleSelectedListener { public void onArticleSelected(Uri articleUri); } ... }
然后,持有这个Fragment的Activity要实现OnArticleSelectedListener接口,并且要重写onArticleSelected()方法把来自Fragment A的事件通知给Fragment B。要确保持有Fragment的Activity实现这个接口, Fragment A 的onAttach()回调方法(当Fragment被添加到Activity时系统调用这个方法)通过类型转换onAttach()传入的Activity来实例化一个OnArticleSelectedListener的实例。
public static class FragmentA extends ListFragment { OnArticleSelectedListener mListener; ... @Override public void onAttach(Activity activity) { super.onAttach(activity); try { mListener = (OnArticleSelectedListener) activity; } catch (ClassCastException e) { throw new ClassCastException(activity.toString() + " must implement OnArticleSelectedListener"); } } ... }
如果这个Activity没有实现这个接口,那么Fragment会抛出ClassCastException异常。如果成功,那么mListener成员就会拥有Activity实现的OnArticleSelectedListener对象的引用,以便Fragment A能够通过OnArticleSelectedListener接口定义的回调方法和Activity共享事件。例如,如果ListFragment继承了Fragment A,那么用户每次点击列表项时,系统都会调用Fragment中的onListItemClick()方法,然后调用onArticleSelected()方法和Activity共享事件:
public static class FragmentA extends ListFragment { OnArticleSelectedListener mListener; ... @Override public void onListItemClick(ListView l, View v, int position, long id) { // Append the clicked item's row ID with the content provider Uri Uri noteUri = ContentUris.withAppendedId(ArticleColumns.CONTENT_URI, id); // Send the event and Uri to the host activity mListener.onArticleSelected(noteUri); } ... }
传递给onListItemClick()的id参数是被点击项目的行ID,Activity(或其他的Fragment)使用这个ID从应用程序的ContentProvider对象中获取对应的文章。
关于使用有效内容提供器的更多内容,请看内容提供器(Content Providers)文档。
注:本人转载系个人觉得翻译的很好,值得收藏,且自己回头看着方便。
如有兴趣请访问作者官方博客http://blog.csdn.net/FireOfStar
相关文章推荐
- Android Fragment用法之给Activity创建事件回调
- Android Fragment---给Activity创建事件回调
- Android Fragment用法之给Activity创建事件回调
- Android Transition 和 Activity & Fragment Transition详解(三)--从创建一个Scene开始
- Android开发之Fragment与Activity的数据交互通过回调机制实现
- Android回调机制之Activity与DialogFragment数据传递
- 使用Android模板创建的tabbed activity项目之问题(can’t convert taba to fragment)
- android使用接口回调把fragment把值传回到Activity
- Android中Fragment多层嵌套时onActivityResult无法正确回调问题的解决方法
- Android 在Activity中响应Fragment中的事件
- Android的Activity与Fragment之二 Activity的回调机制、生命周期、加载模式
- Android程序 类QQ通知常驻在Notification栏 点击Notification正确回调到之前已经放置在后台的Task中的对应Activity,而不是创建它的一个新实例
- Android程序 类QQ通知常驻在Notification栏 点击Notification正确回调到之前已经放置在后台的Task中的对应Activity,而不是创建它的一个新实例
- android小技巧:在activity中实现与绑定的fragment的回调
- android 之 Fragment 中使用 DialogFragment 创建回调
- fragment向activity传递数据时fragment创建一个回调接口
- 安卓入门 使用android创建一个项目 从启动activity中响应按钮事件 启动另一个activity 并传递参数
- Android开发教程之Fragment定义、创建与使用方法详解【包含Activity通讯,事务执行等】
- android 开发之 fragment与activity 创建菜单的区别
- 一、概述 EventBus是一款针对Android优化的发布/订阅事件总线。主要功能是替代Intent,Handler,BroadCast在Fragment,Activity,Service,线程之间