Fragment与Fragment之间交互
来源:互联网 发布:java设置语言编码 编辑:程序博客网 时间:2024/05/16 01:56
装载自:https://developer.android.com/training/basics/fragments/communicating.html
Fragment与Fragment之间交互
首先我们必须知道Fragment是依附于Activity的,所有以Fragment 之间的交互应通过与之关联的 Activity 来完成,两个 Fragment 之间不应直接交互.
1.定义接口方法进行交互
在 Fragment 类中定义一个接口,并在 Activity 中实现。Fragment 在它的 onAttach() 方法生命周期中获取该接口的实现,然后调用接口的方法.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 可以通过调用
mCallback
(OnHeadlineSelectedListener
接口的实例)的 onArticleSelected()
方法(或者接口的方法其它方法)与 Activity 进行消息传递。比如,当用户在frgment中点击一条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中的回调,其宿主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 }}
发送一条消息至Fragment
宿主Activity 能够发送信息至一个fragment,通过findFragmentById()方法获取Fragment的实例,然后直接调用frgment里面的公共方法向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(); } }}
0 0
- Fragment与Fragment之间交互
- Fragment与Fragment之间的数据交互
- Activity与fragment之间交互
- activity与fragment之间的数据交互
- Fagment 与 Fragment之间通信(交互)
- Fragment与Activity之间的数据交互
- Fragment与Activity之间的数据交互
- android Fragment与Activity之间数据交互
- Activity与fragment之间的交互总结
- Fragment之间的交互
- Fragment之间的实时交互
- 关于Fragment之间的交互
- Fragment与Activity交互
- Fragment 与 Activity交互
- Activity与Fragment交互
- Fragment与Activity交互
- Android进阶之Fragment与Activity之间的数据交互
- Activity与Fragment之间的交互(三)
- 策略模式
- perl HTML::TreeBuilder::XPath 说明
- 【docker基础知识】Libcontainer原理
- 访问samba服务器提示无权限使用网络资源
- jqGrid+FastJson+MybatisPlus快速开发分页排序和增删改查。
- Fragment与Fragment之间交互
- Docker入门二构建自己的image
- angular学习(一)—— 概览
- 打破局限 一飞冲天——企业MES迁移上云
- mac上alias永久话设置
- 启动tomcat时 错误: 代理抛出异常 : java.rmi.server.ExportException: Port already in use: 1099的解决办法
- linux下docker的配置文件daemon.json使用示例
- 华为百度导航Sdk黑屏
- c语言中的按位与,与或,异或;