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,下面是关键代 ...
随机推荐
- SqlServer 分区视图实现水平分表
我们都知道在数据库数据量较多的时候,可数据进行水平扩展,如分库,分区,分表(也叫分区)等.对于分表的一个方案,就是使用分区视图实现. 分区视图允许将大型表中的数据拆分成较小的成员表.根据其中一列中的数 ...
- StackExchange.Redis实现Redis发布订阅
由于ServiceStack.Redis最新版已经收费,所以现在大家陆陆续续都换到StackExchange.Redis上了,关于StackExchange.Redis详细可以参看Github htt ...
- HDU 6097 Mindis (计算几何)
题意:给一个圆C和圆心O,P.Q是圆上或圆内到圆心距离相等的两个点,在圆上取一点D,求|PD| + |QD|的最小值 析:首先这个题是可以用三分过的,不过也太,.... 官方题解: 很不幸不总是中垂线 ...
- oracle中的表空间(tablespace)、方案(schema)、段(segment)、区(extent)、块(block)
数据文件和日志文件是数据库中最重要的文件.它们是数据存储的地方.每个数据库至少有一个与之相关的数据文件,通常情况下不只一个,有很多.数据在数据文件中是如何组织的?要了解这些内容我们首先必须理解什么是表 ...
- 编写高质量代码改善C#程序的157个建议——建议136:优先使用后缀表示已有类型的新版本
建议136:优先使用后缀表示已有类型的新版本 加后缀在某些情况下是很奇怪的形式,我们都不愿意看到OrderProcessor2这样的类型.但是,有的时候仍旧有必要这样做.最典型的是FCL中关于数字证书 ...
- 领域模型驱动设计(Domain Driven Design)入门概述 -----DDD 解释
软件开发要干什么: 反映真实世界要自动化的业务流程 解决现实问题 领域Domain Domain特指软件关注的领域 在不能充分了解业务领域的情况下是不可能做出一个好的软件 领域建模 领域模型驱动设计 ...
- 深入理解java虚拟机(十三) Java 即时编译器JIT机制以及编译优化
在部分的商用虚拟机中,Java 程序最初是通过解释器( Interpreter )进行解释执行的,当虚拟机发现某个方法或代码块的运行特别频繁的时候,就会把这些代码认定为“热点代码”.为了提高热点代码的 ...
- XJOI 3578 排列交换/AtCoder beginner contest 097D equal (并查集)
题目描述: 你有一个1到N的排列P1,P2,P3...PN,还有M对数(x1,y1),(x2,y2),....,(xM,yM),现在你可以选取任意对数,每对数可以选取任意次,然后对选择的某对数(xi, ...
- python读取pop3服务器邮件并且下载
# -*- coding: cp936 -*- import poplib import random import os def getmail(): # 蒋辉文拥有该程序权利 你可以随意使用 em ...
- 超级简单的例子说明JAVA PACKET CLASS 和变量之间的关系
一.包PACKET 就是一个文件夹,包下的CLASS互相访问如一个文件. 二.class内部相当于一个DELPHI的calss,静态函数(static )只能访问静态函数. package Mainp ...