Fragment 与 Activity 通信
先说说背景知识:
(From:http://blog.csdn.net/t12x3456/article/details/8119607)
尽管fragment的实现是独立于activity的,可以被用于多个activity,但是每个activity所包含的是同一个fragment的不同的实例。
Fragment可以调用getActivity()方法很容易的得到它所在的activity的对象,然后就可以查找activity中的控件们(findViewById())。例如:
ViewlistView =getActivity().findViewById(R.id.list);同样的,activity也可以通过FragmentManager的方法查找它所包含的frament们。
有时,你可能需要fragment与activity共享事件。一个好办法是在fragment中定义一个回调接口,然后在activity中实现之。
例如,还是那个新闻程序的例子,它有一个activity,activity中含有两个fragment。fragmentA显示新闻标题,fragmentB显示标题对应的内容。fragmentA必须在用户选择了某个标题时告诉activity,然后activity再告诉fragmentB,fragmentB就显示出对应的内容(为什么这么麻烦?直接fragmentA告诉fragmentB不就行了?也可以啊,但是你的fragment就减少了可重用的能力。现在我只需把我的事件告诉宿主,由宿主决定如何处置,这样是不是重用性更好呢?)。
好,原理就是这些,下面是我自己的一个例子。
程序入口类(MainAcivity)继承自FragmentActivity,包含了两个Fragment。第一个FragmentA放一个EditText,第二个FragmentB中嵌入高德的地图MapView。
要实现的功能是在FrangmentB中进行定位,然后把定位得到的经纬度坐标通过MainAcivity传给FragmentA,最终显示在FragmentA的EditText上。
第一步,在FragmentB中定义一个接口,监听定位:
public class FragmentB extends android.app.Fragment {
//Container Activity must implement this interface
public interface OnLocationGotListener { //监听定位是否完成
public void onLocationGot(LatLonPoint latLonPoint);
}
......
然后通过onAttach方法,在MainActivity建立的时候,检查其是否实现了OnLocationGotListener 接口,并对传入的Activity的实例进行类型转换,如下
public class FragmentB extends android.app.Fragment {
OnLocationGotListener onLocationGotListener;
public interface OnLocationGotListener {
public void onLocationGot(LatLonPoint latLonPoint);
}
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
try {
onLocationGotListener = (OnLocationGotListener)activity;
} catch (ClassCastException e) {
throw new ClassCastException(activity.toString()
+ "must implement onLocationGotListener");
}
}
......
然后在FragmentB的相关函数里为接口里定义的方法传值,如下:
/**
* 定位成功后回调函数
*/
@Override
public void onLocationChanged(AMapLocation aLocation) {
if (mListener != null && aLocation != null) {
mListener.onLocationChanged(aLocation);// 显示系统小蓝点
android.util.Log.i("activate","success"); LatLonPoint noteLatLonPoint = new LatLonPoint(aLocation.getLatitude(),aLocation.getLongitude());
onLocationGotListener.onLocationGot(noteLatLonPoint); //将定位得到的经纬度坐标传递给接口的方法
}
}
第二步,MainActivity中实现接口,并在MainActivity中通过getFragmentManager()获取FragmentA中的EditText,为其重新赋值:
public class MainActivity extends FragmentActivity implements FragmentB.OnLocationGotListener {
@Override
public void onLocationGot(LatLonPoint latLonPoint) {
mLatLonPoint = latLonPoint; // 获取坐标
Fragment fragment = getFragmentManager().findFragmentById(R.id.fragment_a); // 获取FragmentA的视图
EditText editText = (EditText)fragment.getView().findViewById(R.id.editText) ; // 获取FragmentA中的editText对象
editText.setText(latLonPoint.toString()); // 给FragmentA中editText赋值
}
}
......
Fragment 与 Activity 通信的更多相关文章
- 关于Fragment与Fragment、Activity通信的四种方式
一直想总结一下Fragment与Fragment.Activity通信的问题,今天有时间一共总结了三种,权当抛砖引玉,如果大家还有更好的方式来实现Fragment和Fragment.Activity的 ...
- Android学习——Fragment与Activity通信(二)
接下来就要到Fragment向Activity传输数据了.主要的思路,就是在Fragment中创建一个回调接口,利用该回调接口实现Fragment向Activity传输数据的功能. 回调函数(接口) ...
- Android学习——Fragment与Activity通信(一)
学会了在Activity中加载Fragment的方法之后,接下来便需要学习Activity和Fragment之间的通信.这一节先学习如何把Activity中的信息传递给Fragment. 基本过程 在 ...
- Android 使用EventBus进行Fragment和Activity通信
本文介绍EventBus的基本使用,以及用于Fragment和Activity之间通信. github地址: https://github.com/greenrobot/EventBus 版本是 Ev ...
- fragment 与activity通信 Bundle 序列化数据 Serializable
1.fragment简单套用(静态调用): 新建一个fragment,其xml文件如下: <LinearLayout xmlns:android="http://schemas.and ...
- android中fragment与activity之间通信原理以及例子
参考文章 http://blog.csdn.net/guozh/article/details/25327685#comments Activity和fragment通信方式一般有3种方法 1.在fr ...
- Fragment间的通信
在网上看到的一篇文章,总结的很好 为了重用Fragment的UI组件,创建的每个Fragment都应该是自包含的.有它自己的布局和行为的模块化组件.一旦你定义了这些可重用的Fragment,你就可以把 ...
- Fragment之间的通信
在本节中,你会学到 1.定义接口 2.实现接口 3.将消息传递给fragment 为了重用Fragment UI 组件,在设计中你应该通过定义每一个fragemnt自己的layout和行为,让frag ...
- Fragment与Acitvity通信
Fragment与Activity通信的方式如下: 一.通过初始化函数提供 1.在动态添加Fragment的过程中,我们在Activity中通过Fragment.setArguments()的方法为F ...
随机推荐
- 第四届蓝桥杯C/C++A组题目:振兴中华
首先把题目贴上来吧! 小明参加了学校的趣味运动会,其中的一个项目是:跳格子. 地上画着一些格子,每个格子里写一个字,如下所示:(也可参见图1) 从我做起振 我做起振兴 做起振兴中 起振兴中华 图1 比 ...
- float闭合(清除浮动)和CSS HACK
一.float 闭合(清除浮动) 将以下代码加入Global CSS 中,给需要闭合的div加上 class="clearfix" 即可,屡试不爽. <style>.c ...
- easyUI中treegrid组件构造树形表格(简单数据类型)+ssm后台
这几天做的项目要求用树形表格的形式展示一部分数据,于是就想到了使用easyUI的treegrid组件,但几经翻查各种资料,发现数据类型大多采取标准数据类型,即包含children元素的数据类型,小编查 ...
- Redis源码研究--跳表
-------------6月29日-------------------- 简单看了下跳表这一数据结构,理解起来很真实,效率可以和红黑树相比.我就喜欢这样的. typedef struct zski ...
- jquery实现密码框显示提示文字
jquery实现密码框提示文字的功能. 代码: <html> <head> 3 <title>登录-jquery实现密码框显示文字-www.jbxue. ...
- Oracle 表的连接方式(2)-----HASH JOIN的基本机制3
HASH JOIN的模式 hash join有三种工作模式,分别是optimal模式,onepass模式和multipass模式,分别在v$sysstat里面有对应的统计信息: SQL> sel ...
- xv6实验环境搭建
安装bochs 因为要运行的是xv6,所以不能直接使用 apt-get 直接获取软件.apt-get获取到的软件不支持SMP (Symmetric Multi-Processing).因此,需要下载源 ...
- 【js】js 让图片旋转
转http://www.cnblogs.com/ustcyc/p/3760116.html 核心: canvas.style.filter = "progid:DXImageTransfo ...
- CustomEditor 自定义预览窗
using UnityEngine; using System.Collections; public class MyTextureView : MonoBehaviour { public Tex ...
- Java加解密与数字签名
** Java加解密 ** 实现方式:JDK实现,CC,BC JDK提供比较基础的底层的实现:CC提供一些简化的操作:BC提供补充 一.Base64加密 非常简单,加密解密就一个函数. 代码如下: 二 ...