5.3 与其他Fragment交互
来源:互联网 发布:t型截面惯性矩的算法 编辑:程序博客网 时间:2024/05/21 09:46
为了让Fragment UI部件能够被重用,Fragment被定义成自包含、模块化的部件,拥有自己的布局和行为。这些Fragment被创建后,可以通过应用逻辑将其与Activity或者其他Fragment相关联从而实现一个复合UI。
当需要一个Fragment与其他Fragment交互时,如基于用户事件的内容修改。所有的Fragment之间的通信都通过其关联的Activity,而不会直接跟对方Fragment通信。
定义接口
为了让Fragment能跟Activity通信,需要为Fragment定义一个接口,并在Activity调用该接口。Fragment在onAttach()生命周期中方法中获取到接口方法,之后就可以通过调用接口中的方法与Activity进行通信。下面展示了一个Fragment与Activity的通讯实现:
public classHeadlinesFragmentextendsListFragment{
OnHeadlineSelectedListener mCallback;
// Container Activity must implement this interface
public interfaceOnHeadlineSelectedListener{
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 newClassCastException(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);
}
实现接口
为了能够让Activity处理Fragment的事件,必须在Activity中在Fragment定义的接口。下面的代码实现了上一小节中定义的接口:
public staticclass MainActivityextends 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(); } } }
- 5.3 与其他Fragment交互
- 与其他APP进行交互
- 与其他应用交互 Intent
- android 与其他程序交互(一)
- PHP与其他语言交互-总结
- IOS与其他客户端音频交互
- Android利用Intent与其他应用交互
- android与其他应用的交互
- 实现自己与其他app交互
- H5与其他平台交互框架
- SAP与其他系统交互的方式
- 【Android training】与其他应用交互 Intent
- [Andriod官方训练教程]使用Fragment创建一个动态的UI之与其他Fragments进行交互
- [Andriod官方训练教程]使用Fragment创建一个动态的UI之与其他Fragments进行交互
- 使用Fragment创建一个动态UI - 与其他Fragment通信
- WMS与其他系统交互处理需要考虑的问题
- app内嵌H5页面与其他app的交互
- Angular4_Checkbox多选框选中事件,与其他组件交互
- 《深度探索C++对象模型》--2 构造函数语意学
- 设计模式23种小结
- Linux学习路线之linux的amr开发学习方法
- maven学习笔记【配置参考】
- 高斯消元法
- 5.3 与其他Fragment交互
- 【VS2010学习笔记】【函数学习】二(SetTimer()函数)
- DOM知识
- Hibernate配置文件hbm主键的generator可选项
- android wifi模块分析
- 排序算法总结-C语言实现
- liunx----putty--ssh--ppk---密文自动登陆
- C/C++野指针
- 源码安装GCC