Fragment间通信
2016-11-01 21:49
288 查看
为了重用Fragment UI组件,您应该将每个组件构建为一个完全自包含的模块化组件,以定义其自己的布局和行为。一旦定义了这些可重用的Fragment,您可以将它们与一个Activity相关联,并将它们与应用程序逻辑连接,以实现整个复合UI。
通常,您将需要一个Fragment与另一个Fragment进行通信,例如根据用户事件更改内容。所有Fragment到Fragment的通信通过相关联的Activity完成。两个Fragment永远不应该直接通信。
这里是Fragment to Activity通信的示例:
现在Fragment可以通过使用OnHeadlineSelectedListener接口的mCallback实例调用onArticleSelected()方法(或接口中的其他方法)来向Activity传递消息。
例如,当用户单击列表项时,将调用Fragment中的以下方法。该Fragment使用回调接口将事件传递给父Activity。
例如,以下Activity实现了上述示例中的接口。
例如,假设上面显示的Activity可能包含另一个Fragment,用于显示由上述回调方法返回的数据指定的项目。在这种情况下,Activity可以将回调方法中接收到的信息传递给将显示项目的其他Fragment:
通常,您将需要一个Fragment与另一个Fragment进行通信,例如根据用户事件更改内容。所有Fragment到Fragment的通信通过相关联的Activity完成。两个Fragment永远不应该直接通信。
定义接口
要允许Fragment与其Activity进行通信,您可以在Fragment类中定义一个接口,并在Activity中实现它。 Fragment在其onAttach()生命周期方法期间捕获接口实现,然后可以调用Interface方法以与Activity进行通信。
这里是Fragment to 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中的以下方法。该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:
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(); } } }
相关文章推荐
- 使用C++实现JNI接口需要注意的事项
- Android IPC进程间通讯机制
- Android Manifest 用法
- [转载]Activity中ConfigChanges属性的用法
- Android之获取手机上的图片和视频缩略图thumbnails
- Android之使用Http协议实现文件上传功能
- Android学习笔记(二九):嵌入浏览器
- android string.xml文件中的整型和string型代替
- i-jetty环境搭配与编译
- android之定时器AlarmManager
- android wifi 无线调试
- Android Native 绘图方法
- Android java 与 javascript互访(相互调用)的方法例子
- android 代码实现控件之间的间距
- android FragmentPagerAdapter的“标准”配置
- Android"解决"onTouch和onClick的冲突问题
- android:installLocation简析
- android searchView的关闭事件
- SourceProvider.getJniDirectories