Android学习路径(23)应用Fragment建立动态UI——Fragment之间的通信
为了要重用Fragment的UI组件。你应该为它们每个都构建一个完整独立的,模块化的组件来定义他自身的布局和行为。
一旦你定义了这些可重用的Fragments。你能够通过activity关联它们同一时候通过应用逻辑连接它们来实现全部复杂的UI。
你通常都希望一个fragment可以和其它的fragment进行交互,比如基于用户的操作改变内容。全部的fragment之间的交流的完毕都通过activity的关联。两个fragment之间一定不要直接交流。
定义一个接口
要同意一个fragment和它的activity通讯。你可以在fragment类中定义一个接口。然后再activity中实现它。
Fragment在onAttach()方法中获取这个接口的实现,这样之后就行调用这些接口来达到和activity通讯的目的。
以下是一个fragment和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中列表的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的回调事件,包括这个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的public方法。
比如,设想上面的那个activity包括还有一个fragment,这个fragment是用来展示通过前面的回调方法返回的数据指定的item。在这个案例中,这个activity能够这个activity能够将接收的这些信息传递给其他的fragment用来显示这个item:
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(); } } }
相关文章
- Xposed框架Hook Android应用的所有类方法打印Log日志
- Android敲诈者病毒“安卓性能激活”分析(2015年9月版)
- Android c/c++ 应用向linux 平台迁移执行
- Android天气预报+百度天气接口
- Android UI 自定义控件大全
- Android应用性能优化最佳实践.1.3 Android Studio实用技巧
- Android应用性能优化最佳实践.2.3 布局优化
- 如何通过使用fiddler对Android系统设备抓包总结
- Android模拟器识别技术
- Android中Fragment的应用
- 快速集成华为 AGConnect 应用内消息服务 - Android
- 《Android 应用案例开发大全(第二版)》——2.1节案例背景及功能概述
- 《Android应用开发入门经典(第3版)》——第6.5节ImageView
- Android Studio出现UnsupportedClassVersionError Unsupported major.minor version 52.0
- Android Material风格的应用(五)--CollapsingToolbar
- Android DrawerLayout 高仿QQ5.2双向侧滑菜单
- Android 仿PhotoShop调色板应用(四) 不同区域颜色选择的颜色生成响应
- 快速接入 Android BLE 开发的基础框架
- OAuth2.0部署不当,数十亿Android App账户存泄露风险
- android 实现2张图片层叠效果
- android应用降低崩溃率手段汇总
- Android 开发死磕性能优化,方向是否正确?
- 《OpenGL ES应用开发实践指南:Android卷》—— 1.3 初始化OpenGL
- Android Studio 默认 debug.keystore , apk打包,keystore.jks文件生成,根据keystore密钥获取SHA1安全码
- Android应用中返回键的监听及处理
- Android Robotium跨应用处理方法
- android游戏物理引擎开发——粒子系统(三)
- Android 中的 Service 全面总结
- Unity 之 Profiler的Android真机测试