Android Fragment之间的通信(用fragment替换掉XML布局文件中的一个线性布局)
1.XML布局
(1)主界面
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
tools:context=".MainActivity"> <LinearLayout
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="match_parent"
android:id="@+id/ll1"
android:orientation="vertical"> </LinearLayout> <LinearLayout
android:id="@+id/ll2"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="vertical"> </LinearLayout>
</LinearLayout>
主界面中的两个LinearLayout各占一半,分别用fragment代替
(2)Fragment界面
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"> <TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="我是tongxunlu模块" /> <Button
android:id="@+id/bt_tongxunlu"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="测试tongxunlu" /> </LinearLayout>
2.java后台
(1)MainActivity.java
package com.example.administrator.test58fragment_tongxin; import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle; public class MainActivity extends AppCompatActivity { @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//1.获取fragment管理者
FragmentManager fragmentManager=getSupportFragmentManager();
//2.开启一个事务
FragmentTransaction fragmentTransaction=fragmentManager.beginTransaction();
//3.替换fragment
fragmentTransaction.replace(R.id.ll1,new TongxunluFragment(),"f1"); //注意:第3个参数用于fragment之间相互传递参数
fragmentTransaction.replace(R.id.ll2,new WoFragment(),"f2"); //4.提交事务
fragmentTransaction.commit(); }
}
(2)TongxunluFragment.java
package com.example.administrator.test58fragment_tongxin;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button; public class TongxunluFragment extends Fragment {
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view=inflater.inflate(R.layout.fragment_tongxunlu,null); //1.找到Fragment中的按钮,设置点击事件
Button bt_tongxunlu=view.findViewById(R.id.bt_tongxunlu);
bt_tongxunlu.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//2.修改WoFragment中的内容,通过fragment的公共桥梁,activity(两个fragment都在activity中创建的)
WoFragment woFragment= (WoFragment) getActivity().getSupportFragmentManager().findFragmentByTag("f2");
woFragment.updateText("hello from tongxunlu");
}
});
return view;
}
}
(3)WoFragment.java
package com.example.administrator.test58fragment_tongxin; import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView; public class WoFragment extends Fragment {
TextView tv_wo;
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view=inflater.inflate(R.layout.fragment_wo,null); //测试fragment中的组件是否可以被点击
Button bt_wo=view.findViewById(R.id.bt_wo);
tv_wo=view.findViewById(R.id.tv_wo);
bt_wo.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
System.out.println("hello wo");
}
});
return view;
} public void updateText(String content){
tv_wo.setText(content);
}
}
3.工程效果

效果说明:点击测试tongxunlu按钮,会修改右侧textview中的内容
Android Fragment之间的通信(用fragment替换掉XML布局文件中的一个线性布局)的更多相关文章
- Android在代码中使用布局文件中的一个组件
使用前必须要把组件与其父组件的关系断开,比如有一个组件的名称为scrollChildLayout,则可以使用下面的代码进行分离 ((ViewGroup)scrollChildLayout.getPar ...
- Fragment之间的通信(四)
自定义两个fragment的布局和java类. 在mainactivity中引用布局文件 在其中的一个fragment中的控件上添加监听,获取到另一个fragment中控件的内容,展示出来完成frag ...
- Android使用Fragment来实现TabHost的功能(解决切换Fragment状态不保存)以及各个Fragment之间的通信
以下内容为原创,转载请注明:http://www.cnblogs.com/tiantianbyconan/p/3360938.html 如新浪微博下面的标签切换功能,我以前也写过一篇博文(http:/ ...
- Android使用Fragment来实现ViewPager的功能(解决切换Fragment状态不保存)以及各个Fragment之间的通信
以下内容为原创,转载请注明:http://www.cnblogs.com/tiantianbyconan/p/3364728.html 我前两天写过一篇博客<Android使用Fragment来 ...
- [转][译][Android基础]Android Fragment之间的通信
2014-2-14 本篇文章翻译自Android官方的培训教程,我也是初学者,觉得官方的Training才是最好的学习材料,所以边学边翻译,有翻译不好的地方,请大家指正. 如果我们在开发过程中为了重用 ...
- Fragment的生命周期&同一Activity下不同Fragment之间的通信
Android开发:碎片Fragment完全解析(2) Fragment的生命周期 和Activity一样,Fragment也有自己的生命周期,理解Fragment的生命周期非常重要,我们通过代码的方 ...
- 使用Broadcast实现android组件之间的通信 分类: android 学习笔记 2015-07-09 14:16 110人阅读 评论(0) 收藏
android组件之间的通信有多种实现方式,Broadcast就是其中一种.在activity和fragment之间的通信,broadcast用的更多本文以一个activity为例. 效果如图: 布局 ...
- 使用Broadcast实现android组件之间的通信
android组件之间的通信有多种实现方式,Broadcast就是其中一种.在activity和fragment之间的通信,broadcast用的更多本文以一个activity为例. 效果如图: 布局 ...
- 在布局文件中使用Fragment的步骤
为了在Activity布局文件中使用Fragment我们需要四个步骤. 1.定义一个Activity,他继承android.support.v4.app.FragmentActivity,下面是关键代 ...
随机推荐
- [SoapUI] 怎样确定一个应答报文的格式是不是标准的JSON
有一个网站 : http://jsonviewer.stack.hu/ 将Response的文本贴进去,如果是标准的JSON格式,就可以以JSON的view显示出来
- js运算浮点数
在js中做小数:9.3+0.3会发现,得到的结果并不是9.6,而是9.600000000000001.这是为什么? Javascript采用了IEEE-745浮点数表示法,这是一种二进制表示法,可以精 ...
- 一个新手后端需要了解的前端核心知识点之position(一)
以下内容是基于观看慕课网视频教程总结的知识点,边打代码边总结,符合自己的思维习惯.不是针对新手入门 我做程序的初衷是想做一个网站出来.HTML语言当然重要啊,缺什么就百度什么,很浪费时间,还是好好的打 ...
- 基于CacheManager组件的缓存产品配置
一.Couchbase 使用CacheManager组件,在配置Couchbase缓存支持时,由于对配置节cache handle命名规则要求不了解,费了点时间查了源码才明白. section配置节 ...
- jQuery自动截取文字长度,超过部分
<html> <head> <meta charset="utf-8"> <script src="js/jqu ...
- 搜狐 WEB 标准-前端技术应用规范
- linux系统学习(一)
查看linux版本信息:http://distrowatch.com/ linux根目录下系统文件夹的含义 /boot 该目录下默认存放的是linux的启动文件和内核,一般200M swap交换分区, ...
- 编写高质量代码改善C#程序的157个建议——建议140:使用默认的访问修饰符
建议140:使用默认的访问修饰符(我不太赞成作者的这个观点,这样减少的代码基本可以忽略不计,但是,如果把访问修饰符补充完整,反而会使代码更加易读.我认为自己写代码时应该尽量加上访问修饰符,看别人写的代 ...
- 一、Numpy基础--数组
(一)Numpy数组对象 Numpy中的nadrray是一个多维数组对象,该对象由两部分组成: 实际的数据 描述这些数据的元数据 大部分的数组操作仅仅修改元数据部分,而不改变底层的实际数据. 数组的数 ...
- Java 理论与实践: 修复 Java 内存模型,第 2 部分(转载)
在 JSR 133 中 JMM 会有什么改变? 活跃了将近三年的 JSR 133,近期发布了关于如何修复 Java 内存模型(Java Memory Model, JMM)的公开建议.在本系列文章的 ...