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. cacti监控mssql 2005运行资源情况

    概述:SQL Server2000\2005\2008本身不支持snmp,使用cacti监控mssql,必须通过php连接mssql来获取SQL 2005性能计算器的值. 操作步骤: 1.php连接m ...

  2. Oracle 11g 重建EM需要删除的对象

    因为需求需要重建EM,重建时因为某些错误被迫停止,比如对象已存在.用户已经存在等,最终找出了创建必备的条件: 1.环境变量(Oracle和Grid在同一个用户下安装) ORACLE_HOME 要设为D ...

  3. Spring MVC 小计

    前端控制器是DispatcherServlet 应用控制器     1 处理器映射器(Handler Mapping)   进行处理器管理 2 视图解析器(View Resolver)         ...

  4. HUST软工1501-1503班第4周作业成绩公布

    说明 本次公布的成绩为第四周作业的结果: 第4周小组作业:WordCount优化 如果同学对作业结果存在异议,可以: 在毕博平台讨论区的第4周在线答疑区发帖申诉. 或直接在博客园本帖中进行评论进行申诉 ...

  5. Ubuntu14.04-LTS 从系统安装到配置可用

    1.安装Ubuntu14.04LTS-64bit 使用U盘安装很方便快捷,可以使用老毛桃使用iso模式制作一个U盘启动盘,然后分区安装. 如果使用硬盘安装的话需要注意的问题是: 如果电脑上以前有Lin ...

  6. java轻量级IOC框架Guice(转)

    出处:http://www.cnblogs.com/whitewolf/p/4185908.html Guice是由Google大牛Bob lee开发的一款绝对轻量级的java IoC容器.其优势在于 ...

  7. UVALive 7749 Convex Contour (计算几何)

    题意:给定上正方形,圆,三角形,让你求出包围它的最短的路径. 析:首先,如果是这种情况  三角形 三角形 三角形 正方形(圆) 三角形 三角形 三角形 ..这一种就是直接从左边直接连到正方形(圆),也 ...

  8. perl读取excel

    因为工作当中遇到要处理大数据的excel的玩意,最多的有几十万行.用perl的方式试试,看看效果如何. ppm install OLE::Storage_Lite #如果不安装这个,后面两个安装不了 ...

  9. mysql数据库引擎MyISAM与InnoDB的区别浅说

    mysql的存储引擎包括:MyISAM.InnoDB.BDB.MEMORY.MERGE.EXAMPLE.NDBCluster.ARCHIVE.CSV.BLACKHOLE.FEDERATED等,其中In ...

  10. MVC4 View 的呈现

    一 ActionResult: 1. EmptyResult: Action方法返回的ActionResult对象被ActionInvoker 调用以实现对当前请求的响应,不论Action方法是否具有 ...