Fragment之间的通信(四)
- 自定义两个fragment的布局和java类。
- 在mainactivity中引用布局文件
- 在其中的一个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之间的通信(四)的更多相关文章
- Android使用Fragment来实现ViewPager的功能(解决切换Fragment状态不保存)以及各个Fragment之间的通信
以下内容为原创,转载请注明:http://www.cnblogs.com/tiantianbyconan/p/3364728.html 我前两天写过一篇博客<Android使用Fragment来 ...
- Android使用Fragment来实现TabHost的功能(解决切换Fragment状态不保存)以及各个Fragment之间的通信
以下内容为原创,转载请注明:http://www.cnblogs.com/tiantianbyconan/p/3360938.html 如新浪微博下面的标签切换功能,我以前也写过一篇博文(http:/ ...
- Fragment的生命周期&同一Activity下不同Fragment之间的通信
Android开发:碎片Fragment完全解析(2) Fragment的生命周期 和Activity一样,Fragment也有自己的生命周期,理解Fragment的生命周期非常重要,我们通过代码的方 ...
- [转][译][Android基础]Android Fragment之间的通信
2014-2-14 本篇文章翻译自Android官方的培训教程,我也是初学者,觉得官方的Training才是最好的学习材料,所以边学边翻译,有翻译不好的地方,请大家指正. 如果我们在开发过程中为了重用 ...
- Android中使用开源框架EventBus3.0实现Fragment之间的通信交互
1.概述 在之前的博文中简单介绍过如何实现fragment之间的信息交互:<Android中Fragment与Activity之间的交互(两种实现方式)>,今天继续给大家介绍一种可以实现此 ...
- Fragment之间的通信
在本节中,你会学到 1.定义接口 2.实现接口 3.将消息传递给fragment 为了重用Fragment UI 组件,在设计中你应该通过定义每一个fragemnt自己的layout和行为,让frag ...
- Android - Fragment (三)不同Fragment之间的通信
在Fragment的java文件中,可以使用getActivity()来获得调用它的activity, 然后再找到另一个Fragment,进行通信 getActivity().getFragmentM ...
- 利用Fragment创建动态UI 之 Fragment之间的通信
为了可以复用一个fragment,所以在定义fragment的时候,要把它定义为一个完全独立和模块化,它有它自己的layout和行为.当你定义好了这些可复用的fragment,可以把他们和activi ...
- Android学习路径(23)应用Fragment建立动态UI——Fragment之间的通信
为了要重用Fragment的UI组件.你应该为它们每个都构建一个完整独立的,模块化的组件来定义他自身的布局和行为. 一旦你定义了这些可重用的Fragments.你能够通过activity关联它们同一时 ...
随机推荐
- Java界面
JFrame.java package myProject; import java.awt.Color;import java.awt.EventQueue; import javax.swing. ...
- unity-点乘和叉乘的应用
http://blog.csdn.net/oskytonight/article/details/38900087 点乘:两个向量点乘得到一个标量 ,数值等于两个向量长度相乘后再乘以二者夹角的余弦值 ...
- Apache Commons Collections
http://commons.apache.org/proper/commons-collections/userguide.html 1. Utilities SetUtils Collection ...
- (转)assert 断言式编程
编写代码时,我们总是会做出一些假设,断言就是用于在代码中捕捉这些假设,可以将断言看作是异常处理的一种高级形式.断言表示为一些布尔表达式,程序员相信在程序中的某个特定点该表达式值为真.可以在任何时候启用 ...
- 《开源安全运维平台:OSSIM最佳实践》内容简介
<开源安全运维平台:OSSIM最佳实践 > 李晨光 著 清华大学出版社出版 内 容 简 介在传统的异构网络环境中,运维人员往往利用各种复杂的监管工具来管理网络,由于缺乏一种集成安全运维平台 ...
- jmeter for循环嵌套if学习2
if语句中勾选Evaluate选项,每执行一句都会判断result的值是否为true. 执行结果: three没有执行,到debug时变量的值变成tom了
- WordPress登陆页和后台面空白解决方法
真没想到我居然也会碰到这么蛋疼的事情,有一天我登陆博客,输入账号密码之后登陆没有反应,之后我就试着用首页前台登陆(因为这个模板前台带登陆功能),之后成功登陆进入后台更新文章.我想算了.这小毛病就丢那吧 ...
- C++ 多态、虚函数机制以及虚函数表
1.非virtual函数,调用规则取决于对象的显式类型.例如 A* a = new B(); a->display(); 调用的就是A类中定义的display().和对象本体是B无关系. 2. ...
- Java注解入门
注解的分类 按运行机制分: 源码注解:只在源码中存在,编译后不存在 编译时注解:源码和编译后的class文件都存在(如@Override,@Deprecated,@SuppressWarnin ...
- 一个会Flash的人来说CSS3
以前在上大学的时候学过Flash,而且以前做一些网页效果都是用的Flash,现在才知道以前我所用的方法是有多麻烦.在学过CSS3之后,我发现以前要用Flash写半天的效果,现在用几句CSS3代码就搞定 ...