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布局文件中的一个线性布局)的更多相关文章

  1. Android在代码中使用布局文件中的一个组件

    使用前必须要把组件与其父组件的关系断开,比如有一个组件的名称为scrollChildLayout,则可以使用下面的代码进行分离 ((ViewGroup)scrollChildLayout.getPar ...

  2. Fragment之间的通信(四)

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

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

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

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

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

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

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

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

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

  7. 使用Broadcast实现android组件之间的通信 分类: android 学习笔记 2015-07-09 14:16 110人阅读 评论(0) 收藏

    android组件之间的通信有多种实现方式,Broadcast就是其中一种.在activity和fragment之间的通信,broadcast用的更多本文以一个activity为例. 效果如图: 布局 ...

  8. 使用Broadcast实现android组件之间的通信

    android组件之间的通信有多种实现方式,Broadcast就是其中一种.在activity和fragment之间的通信,broadcast用的更多本文以一个activity为例. 效果如图: 布局 ...

  9. 在布局文件中使用Fragment的步骤

    为了在Activity布局文件中使用Fragment我们需要四个步骤. 1.定义一个Activity,他继承android.support.v4.app.FragmentActivity,下面是关键代 ...

随机推荐

  1. 洛谷 P3112 [USACO14DEC]后卫马克Guard Mark

    题目描述 Farmer John and his herd are playing frisbee. Bessie throws the frisbee down the field, but it' ...

  2. easyui combogrid 多选加载,保存,显示代码

    1.调用代码 UTIL.SetDict($("#txt_ExcludeIndustry_"), "SECTOR_TYPE", true, true, funct ...

  3. 运行Hadoop start-all.sh遇到的错误ssh: connect to host localhost port 22: Connection refused

    ssh: connect to host localhost port 22: Connection refused 我的情况是ssh server没装,查看方法: ps -e |grep ssh 1 ...

  4. 05 Computing GC Content

    Problem The GC-content of a DNA string is given by the percentage of symbols in the string that are ...

  5. Qt5.3.1,的linux平台体验之旅

    1. samba安装:http://blog.csdn.net/voice_shen/article/details/7692605 2. 安装run,  sudo chmod 777 filenam ...

  6. C# 静态类的使用

    静态类与非静态类基本相同,但存在一个区别:静态类不能实例化.也就是说,不能使用 new 关键字创建静态类类型的变量.因为没有实例变量,所以要使用类名本身访问静态类的成员. static class C ...

  7. UVa 1608 Non-boring sequences (分治)

    题意:给你一个长度为n序列,如果这个任意连续子序列的中都有至少出现一次的元素,那么就称这个序列是不无聊的,判断这个序列是不是无聊的. 析:首先如果整个序列中有一个只出过一次的元素,假设是第 p 个,那 ...

  8. “undefined reference to JNI_GetCreatedJavaVM”和“File format not recognized”错误原因分析

    "undefined reference to JNI_GetCreatedJavaVM"和"File format not recognized"错误原因分析 ...

  9. 编写高质量代码改善C#程序的157个建议——建议151:使用事件访问器替换公开的事件成员变量

    建议151:使用事件访问器替换公开的事件成员变量 事件访问器包含两部分内容:添加访问器和删除访问器.如果涉及公开的事件字段,应该始终使用事件访问器.代码如下所示: class SampleClass ...

  10. java学习(三)数组

    一维数组的定义格式: int[] a;  //定义一个int类型的数组a变量 int a[];  //定义一个int类型的a数组变量 初始化一个int类型的数组 int[]   arr = new i ...