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();
- }
- }
- }
Android学习路径(23)应用Fragment建立动态UI——Fragment之间的通信的更多相关文章
- Android学习路径(22)应用Fragment建立动态UI——构建一个灵活UI
当你设计你的应用来支持多个屏幕尺寸.你能够基于可用的屏幕空间通过在不同的布局上重用fragment来优化用户体验. 比如,在一个手机上.使用单面板(一次仅仅显示一个fragment)的用户体验更加合适 ...
- 9) 十分钟学会android--使用Fragment建立动态UI
为了在 Android 上为用户提供动态的.多窗口的交互体验,需要将 UI 组件和 Activity 操作封装成模块进行使用,这样我们就可以在 Activity 中对这些模块进行切入切出操作.可以用 ...
- Android学习路径(七)建立Action Bar
在action bar最今本的形式中,它只在左边展示了activity的标题以及应用的icon. 即使在这样的简单的形式中,它也不过告诉用户如今在应用的哪个activity中,同一时候为你的应用保持一 ...
- Android学习路线(二十四)ActionBar Fragment运用最佳实践
转载请注明出处:http://blog.csdn.net/sweetvvck/article/details/38645297 通过前面的几篇博客.大家看到了Google是怎样解释action bar ...
- Android学习路线(二十一)运用Fragment构建动态UI——创建一个Fragment
你能够把fragment看成是activity的模块化部分.它拥有自己的生命周期,接受它自己的输入事件,你能够在activity执行时加入或者删除它(有点像是一个"子activity&quo ...
- Android学习系列(23)--App主界面实现
在上篇文章<Android学习系列(22)--App主界面比较>中我们浅略的分析了几个主界面布局,选了一个最大众化的经典布局.今天我们就这个经典布局,用代码具体的实现它. 1.预览图先看下 ...
- 我的android学习经历23
学习fragment时遇到的问题 这几天学习fragment静态加载时遇到这样的问题: java.lang.RuntimeException: Unable to start activity Com ...
- 使用Fragment 实现动态UI 和 动态添加Fragment
首先写好每个Fragment: 1.在第一个Fragment写一个按钮,使其加载下一个Fragment 布局: <LinearLayout xmlns:android="http:// ...
- Android学习笔记(九)一个例子弄清Service与Activity通信
上一篇博文主要整理了Service的创建.绑定过程,本篇主要整理一下Service与Activity的通信方式.包括在启动一个Service时向它传递数据.怎样改变运行中的Service中得数据和侦听 ...
随机推荐
- Swift - 使用arc4random()、arc4random_uniform()取得随机数
arc4random()这个全局函数会生成9位数的随机整数 1,下面是使用arc4random函数求一个1~100的随机数(包括1和100) 1 var temp:Int = Int(arc4ra ...
- TCP、UDP数据包大小的限制(UDP数据包一次发送多大为好)——数据帧的物理特性决定的,每层都有一个自己的数据头,层层递减
1.概述 首先要看TCP/IP协议,涉及到四层:链路层,网络层,传输层,应用层. 其中以太网(Ethernet)的数据帧在链路层 IP包在网络层 TCP或UDP包在传输层 TCP或UDP中的数据(Da ...
- Automatic logon configuration on Linux OS
Automatic logon configuration on Linux OS 1. Regarding to DSA: a) ssh-keygen -t dsa b) cat ~/.ssh/i ...
- ACM-简单题之Factorial——poj1401
转载请注明出处:http://blog.csdn.net/lttree Factorial Time Limit: 1500MS Memory Limit: 65536K Total Submis ...
- django中mysql数据库设置错误解决方法
刚在django中settings.py进行设置mysql数据库. 当进行执行python manage.py shell命令时会报以下错误: 只需要在settings.py中 DATABASES = ...
- 纯C语言INI文件解析
原地址:http://blog.csdn.net/foruok/article/details/17715969 在一个跨平台( Android .Windows.Linux )项目中配置文件用 IN ...
- 解决ScrollView中的ListView无法显示全
问题描述: ListView加入到ScrollView中之后,发现只能显示其中一条,具体原因得看一下源代码.现在先贴一下方案 (转自:http://blog.csdn.net/hitlion2008/ ...
- Eclipse乱码怎么办
Eclipse里设置编码有三个层次:全局.工程.文件. 文件的编码会覆盖工程的编码,工程的编码会覆盖全局的编码. 我猜测:虽然你的工程编码更改为GBK,但只对新建文件有效. 如果工程中旧有的文件是UT ...
- SPOJ DISUBSTR(后缀数组)
传送门:DISUBSTR 题意:给定一个字符串,求不相同的子串. 分析:对于每个sa[i]贡献n-a[i]个后缀,然后减去a[i]与a[i-1]的公共前缀height[i],则每个a[i]贡献n-sa ...
- 每天一点儿java-button
<pre name="code" class="java">import java.awt.*; import java.awt.event.*; ...