Android Fragment——跟Activity通信
来源:互联网 发布:linux select事件类型 编辑:程序博客网 时间:2024/06/10 04:12
View listView = getActivity().findViewById(R.id.list);
同样Activity通过从FragmentManager中获得的Fragment引用也能够调用Fragment中的方法,使用findFragmentById()或findFragmentByTag()方法获取Fragment引用,例如:
ExampleFragment fragment = (ExampleFragment) getFragmentManager().findFragmentById(R.id.example_fragment);
在某些案例中,可能需要Fragment与Activity共享事件。在Fragment内部定义一个回调接口是一个好方法,并且规定由持有它的Activity实现这个回调方法。当Activity通过接口接受回调时,它能在必要时与布局中的其他Fragment共享信息。
例如,如果一个新闻类的应用程序在一个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对象中获取对应的文章。
- Android Fragment——跟Activity通信
- Android Fragment---跟Activity通信
- Android Fragment---跟Activity通信
- Android Fragment---跟Activity通信
- Android--Fragment(2)——Fragment与Activity通信
- 详解Fragment跟Activity之间的通信
- Fragment跟Activity之间的通信
- 详解Fragment跟Activity之间的通信
- Android ——fragment 与Activity 之间的通信
- 【Android】Fragment与Activity通信
- Fragment与Activity通信 Android
- Android入门——Fragment详解之Fragment与Activity通信及数据交换(二)
- Fragment——Activity与Fragment之间进行通信
- Fragment跟Activity之间的通信(互相调用方法)
- Android入门(32)——第九章 Fragment与Activity通信
- Android——Fragment和Activity之间的通信+Frangment生命周期
- android中fragment与activity之间通信
- android Activity与Fragment之间的通信
- CSS文件及样式命名规范
- clientX/clientY 与 screenX/screenY 的区别
- Android Application类的详细介绍
- java反射机制初探
- IOS消息转发机制
- Android Fragment——跟Activity通信
- 内联函数、宏
- Kail Linux渗透测试教程之Recon-NG框架
- mybatis 执行批量更新batch update 的方法(oracle,mysql)
- radio/checkbox/select使用JQurey的常见操作
- QT 创建子窗体
- java之学习多线程(一)
- How Hash Algorithms Work
- COGS 992 宝物筛选 裸多重背包