1. 自定义两个fragment的布局和java类。
  2. 在mainactivity中引用布局文件
  3. 在其中的一个fragment中的控件上添加监听,获取到另一个fragment中控件的内容,展示出来完成fragment之间的通信。

    上代码:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#00FF00"
>

<TextView
android:id="@+id/tv_fragment1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="我是fragment1的数据"
android:textColor="#000000"
android:textSize="25sp"
/>

</LinearLayout>

package com.cm.communicationbetweenfragments;

import android.app.Activity;
import android.app.Fragment;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup; /**
* Created by Administrator on 2016/1/2.
*/
public class Fragment1 extends Fragment { @Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { return inflater.inflate(R.layout.fragment1,container,false);
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent" android:background="#FFFE00"
> <TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="我是fragment2的数据"
android:textSize="25sp"
android:textColor="#000000"
/> <Button
android:id="@+id/btn_fragment2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="点击我可以获取fragment1的数据哦"
android:textColor="#000000"
android:textSize="20sp"
/> </LinearLayout>
package com.cm.communicationbetweenfragments;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView; /**
* Created by Administrator on 2016/1/2.
*/
public class Fragment2 extends Fragment1 { Button btn;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { return inflater.inflate(R.layout.fragment2,container,false);
} @Override
public void onStart() {
super.onStart();
btn=(Button)getActivity().findViewById(R.id.btn_fragment2);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
TextView tv_fragment1=(TextView)getActivity().findViewById(R.id.tv_fragment1);
btn.setText(tv_fragment1.getText());
//Toast.makeText(getActivity(),"获取fragment1的数据",Toast.LENGTH_SHORT).show();
}
});
}
}
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" tools:context=".MainActivity"
> <fragment
android:name="com.cm.communicationbetweenfragments.Fragment1"
android:id="@+id/fragment1"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="match_parent"
/>
<fragment
android:name="com.cm.communicationbetweenfragments.Fragment2"
android:id="@+id/fragment2"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="match_parent"
/>
</LinearLayout>
点击之前:


点击之后:


从而实现了两个fragment之间的通信。

Fragment之间的通信(四)的更多相关文章

  1. Android使用Fragment来实现ViewPager的功能(解决切换Fragment状态不保存)以及各个Fragment之间的通信

    以下内容为原创,转载请注明:http://www.cnblogs.com/tiantianbyconan/p/3364728.html 我前两天写过一篇博客<Android使用Fragment来 ...

  2. Android使用Fragment来实现TabHost的功能(解决切换Fragment状态不保存)以及各个Fragment之间的通信

    以下内容为原创,转载请注明:http://www.cnblogs.com/tiantianbyconan/p/3360938.html 如新浪微博下面的标签切换功能,我以前也写过一篇博文(http:/ ...

  3. Fragment的生命周期&同一Activity下不同Fragment之间的通信

    Android开发:碎片Fragment完全解析(2) Fragment的生命周期 和Activity一样,Fragment也有自己的生命周期,理解Fragment的生命周期非常重要,我们通过代码的方 ...

  4. [转][译][Android基础]Android Fragment之间的通信

    2014-2-14 本篇文章翻译自Android官方的培训教程,我也是初学者,觉得官方的Training才是最好的学习材料,所以边学边翻译,有翻译不好的地方,请大家指正. 如果我们在开发过程中为了重用 ...

  5. Android中使用开源框架EventBus3.0实现Fragment之间的通信交互

    1.概述 在之前的博文中简单介绍过如何实现fragment之间的信息交互:<Android中Fragment与Activity之间的交互(两种实现方式)>,今天继续给大家介绍一种可以实现此 ...

  6. Fragment之间的通信

    在本节中,你会学到 1.定义接口 2.实现接口 3.将消息传递给fragment 为了重用Fragment UI 组件,在设计中你应该通过定义每一个fragemnt自己的layout和行为,让frag ...

  7. Android - Fragment (三)不同Fragment之间的通信

    在Fragment的java文件中,可以使用getActivity()来获得调用它的activity, 然后再找到另一个Fragment,进行通信 getActivity().getFragmentM ...

  8. 利用Fragment创建动态UI 之 Fragment之间的通信

    为了可以复用一个fragment,所以在定义fragment的时候,要把它定义为一个完全独立和模块化,它有它自己的layout和行为.当你定义好了这些可复用的fragment,可以把他们和activi ...

  9. Android学习路径(23)应用Fragment建立动态UI——Fragment之间的通信

    为了要重用Fragment的UI组件.你应该为它们每个都构建一个完整独立的,模块化的组件来定义他自身的布局和行为. 一旦你定义了这些可重用的Fragments.你能够通过activity关联它们同一时 ...

随机推荐

  1. Java_I/O输入输出_实现读取文件时出现一个表示读取进度的进度条。可以使用java.swing包提供的输入流类ProgressMonitorInputStream

    import java.io.*; import javax.swing.*; public class Student { public static void main(String[] temp ...

  2. node.js的优缺点

    node.js的优缺点 优点: 1. 采用事件驱动,异步编程,为网络服务而设计. 2. node.js非阻塞模式的IO处理给node.js带来在相对较低的资源耗用下的高性能与出众的负载能力. 3. n ...

  3. 渐变算法的 Java 实现

    /** * 指定长度的渐变. * @param c0 起始颜色. * @param c1 结束颜色. * @param len 受限的渐变长度,为保证每一个颜色都不一样,会根据颜色找出长度最大值. * ...

  4. WINDOWS7(vs2012+wdk7.6) 配置驱动开发环境

    合肥程序员群:49313181.    合肥实名程序员群:128131462 (不愿透露姓名和信息者勿加入) Q  Q:408365330     E-Mail:egojit@qq.com 1.新建C ...

  5. 运行DbVisualizer报the java_home environment viariable does not point to a working 32-bit JDK OR JRE错误

    报这个错误的根本原因就是DbVisualizer和JDK的版本不一致,那么知道原因,修改起来就方便了,要么修改DbVisualizer的版本,要么修改JDK的版本. 1.JDK7 64位 那么就在Db ...

  6. CentOS5.4 搭建Hadoop2.5.2伪分布式环境

    简介: Hadoop是处理大数据的主要工具,其核心部分是HDFS.MapReduce.为了学习的方便,我在虚拟机上搭建了一个伪分布式环境,来进行开发学习. 一.安装前准备: 1)linux服务器:Vm ...

  7. 虚拟化 模板制作 rhev笔记

    查看主机是否支持RHEV:一要支持vt-x, 一定是64位操作系统 cat /proc/cpuinfo 查看是否有lm(64位) 是否有, vmx(or svm)(vt-x) 虚拟化中制作Linux模 ...

  8. jQuery.attr() 函数详解

    一,jQuery.attr()  函数详解: http://www.365mini.com/page/jquery-attr.htm 二,jQuery函数attr()和prop()的区别: http: ...

  9. ROS机器人语音交互(一)

    语音交互早期已经广泛应用在手机端,电脑端,随着技术的成熟,接口逐渐开放,ROS上老外搞的开源语音识别只支持英文,识别率还低. 国内语音识别技术已经相当成熟稳定.感谢ros小课堂的讲解,解决了自己的疑惑 ...

  10. 转-httpd 2.4.4 + mysql-5.5.28 + php-5.4.13编译安装过程

    一.编译安装apache 1.解决依赖关系 httpd-2.4.4需要较新版本的apr和apr-util,因此需要事先对其进行升级.升级方式有两种,一种是通过源代码编译安装,一种是直接升级rpm包.这 ...