效果图如下:

项目结构图如下:

Fragment1:

package com.demo.fragmenttongxin;

import android.app.Activity;
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button; public class Fragment1 extends Fragment { @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
final View view = inflater.inflate(R.layout.fragment1, null); //找到按钮
Button updBtn = view.findViewById(R.id.btn_update); updBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) { Fragment2 fragment2 = (Fragment2) getActivity().getFragmentManager().findFragmentByTag("f2"); //修改2
fragment2.updateText("修改了哦"); }
}); return view; }
}

Fragment2:

package com.demo.fragmenttongxin;

import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView; public class Fragment2 extends Fragment {
TextView tv_content; @Override
public View onCreateView(LayoutInflater inflater,ViewGroup container, Bundle savedInstanceState) { View view = inflater.inflate(R.layout.fragment2, null);
//找到tv
tv_content = view.findViewById(R.id.tv_content);
return view; } //修改textView的内容
public void updateText(String content){
tv_content.setText(content); } }

MainActivity:

package com.demo.fragmenttongxin;

import android.app.FragmentManager;
import android.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); //获取fragment管理者
FragmentManager fragmentManager = getFragmentManager(); //2.开启一个事务
FragmentTransaction beginTransaction = fragmentManager.beginTransaction(); beginTransaction.replace(
R.id.ll1,new Fragment1(),"f1"
); beginTransaction.replace(
R.id.ll2,new Fragment2(),"f2"
); //3.开启事务
beginTransaction.commit(); }
}

activity_main.xml:

<?xml version="1.0" encoding="utf-8"?>
<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"
android:orientation="horizontal"
tools:context=".MainActivity"> <LinearLayout
android:id="@+id/ll1"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="match_parent"> </LinearLayout> <LinearLayout
android:id="@+id/ll2"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="match_parent"> </LinearLayout> </LinearLayout>

fragment1.xml:

<?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"
> <Button
android:id="@+id/btn_update"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="修改"
/> </LinearLayout>

fragment2.xml:

<?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"> <TextView
android:id="@+id/tv_content"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="hello world !" /> </LinearLayout>

最新文章

  1. POI3.8解决导出大数据量excel文件时内存溢出的问题
  2. Java (JVM) Memory Model – Memory Management in Java
  3. Holding Bin-Laden Captive!_hdu_1085(DP).java
  4. Android ImageButton的背景(图片)大小
  5. svn: E180001: Unable to open an ra_local session to URL问题解决方案
  6. Azure Traffic Manager 现可与 Azure 网站集成!
  7. Java面试13|算法
  8. JavaScript 定义 类
  9. Problem B: 农夫果园 简单点,出题的方式简单点
  10. Mac OS X更新VirtualBox以后Genymotion无法启动的一种情况
  11. Java中HashMap底层实现原理(JDK1.8)源码分析
  12. 【系统架构】软件核心复杂性应对之道-领域驱动DDD(Domain-Driven Design)
  13. LINUX 常用命令(一)
  14. BZOJ.4402.Claris的剑(组合 计数)
  15. 尚硅谷springboot学习3-helloworld程序
  16. hdu4742
  17. 基于Webkit的浏览器关键渲染路径介绍
  18. 微软职位内部推荐-Software Development Engineer II_Commerce
  19. 文件上传submit、ajax方式
  20. 成为java高级程序员需要掌握哪些

热门文章

  1. FortiGate日常检查
  2. Eclipse常用快捷键(用到想到随时更新)
  3. cmp指令
  4. kalman滤波(二)---扩展kalman滤波[EKF]的推导
  5. protobuff 编译注意事项
  6. 《笨方法学Python》加分题1
  7. Transform Model
  8. 走进JDK(七)------LinkedList
  9. verilog HDL-并行语句之assign
  10. IntelliJ IDEA 2018.1.3 破解方法之一