效果图如下:

项目结构图如下:

Fragment1:

  1. package com.demo.fragmenttongxin;
  2. import android.app.Activity;
  3. import android.app.Fragment;
  4. import android.os.Bundle;
  5. import android.view.LayoutInflater;
  6. import android.view.View;
  7. import android.view.ViewGroup;
  8. import android.widget.Button;
  9. public class Fragment1 extends Fragment {
  10. @Override
  11. public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
  12. final View view = inflater.inflate(R.layout.fragment1, null);
  13. //找到按钮
  14. Button updBtn = view.findViewById(R.id.btn_update);
  15. updBtn.setOnClickListener(new View.OnClickListener() {
  16. @Override
  17. public void onClick(View v) {
  18. Fragment2 fragment2 = (Fragment2) getActivity().getFragmentManager().findFragmentByTag("f2");
  19. //修改2
  20. fragment2.updateText("修改了哦");
  21. }
  22. });
  23. return view;
  24. }
  25. }

Fragment2:

  1. package com.demo.fragmenttongxin;
  2. import android.app.Fragment;
  3. import android.os.Bundle;
  4. import android.view.LayoutInflater;
  5. import android.view.View;
  6. import android.view.ViewGroup;
  7. import android.widget.TextView;
  8. public class Fragment2 extends Fragment {
  9. TextView tv_content;
  10. @Override
  11. public View onCreateView(LayoutInflater inflater,ViewGroup container, Bundle savedInstanceState) {
  12. View view = inflater.inflate(R.layout.fragment2, null);
  13. //找到tv
  14. tv_content = view.findViewById(R.id.tv_content);
  15. return view;
  16. }
  17. //修改textView的内容
  18. public void updateText(String content){
  19. tv_content.setText(content);
  20. }
  21. }

MainActivity:

  1. package com.demo.fragmenttongxin;
  2. import android.app.FragmentManager;
  3. import android.app.FragmentTransaction;
  4. import android.support.v7.app.AppCompatActivity;
  5. import android.os.Bundle;
  6. public class MainActivity extends AppCompatActivity {
  7. @Override
  8. protected void onCreate(Bundle savedInstanceState) {
  9. super.onCreate(savedInstanceState);
  10. setContentView(R.layout.activity_main);
  11. //获取fragment管理者
  12. FragmentManager fragmentManager = getFragmentManager();
  13. //2.开启一个事务
  14. FragmentTransaction beginTransaction = fragmentManager.beginTransaction();
  15. beginTransaction.replace(
  16. R.id.ll1,new Fragment1(),"f1"
  17. );
  18. beginTransaction.replace(
  19. R.id.ll2,new Fragment2(),"f2"
  20. );
  21. //3.开启事务
  22. beginTransaction.commit();
  23. }
  24. }

activity_main.xml:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. xmlns:tools="http://schemas.android.com/tools"
  4. android:layout_width="match_parent"
  5. android:layout_height="match_parent"
  6. android:orientation="horizontal"
  7. tools:context=".MainActivity">
  8. <LinearLayout
  9. android:id="@+id/ll1"
  10. android:layout_width="0dp"
  11. android:layout_weight="1"
  12. android:layout_height="match_parent">
  13. </LinearLayout>
  14. <LinearLayout
  15. android:id="@+id/ll2"
  16. android:layout_width="0dp"
  17. android:layout_weight="1"
  18. android:layout_height="match_parent">
  19. </LinearLayout>
  20. </LinearLayout>

fragment1.xml:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:layout_width="match_parent"
  4. android:layout_height="match_parent"
  5. android:orientation="vertical"
  6. >
  7. <Button
  8. android:id="@+id/btn_update"
  9. android:layout_width="wrap_content"
  10. android:layout_height="wrap_content"
  11. android:text="修改"
  12. />
  13. </LinearLayout>

fragment2.xml:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:layout_width="match_parent"
  4. android:layout_height="match_parent">
  5. <TextView
  6. android:id="@+id/tv_content"
  7. android:layout_width="wrap_content"
  8. android:layout_height="wrap_content"
  9. android:text="hello world !"
  10. />
  11. </LinearLayout>

Android的Fragment中的互相通信-桥梁activity的更多相关文章

  1. android,在fragment中使用listview,不使用listfragment

    public class LeftFragment extends Fragment{ private ListView listView; @Override public View onCreat ...

  2. Android 在Fragment中执行onActivityResult不被调用的简单解决方法

    在Android开发中,我们经常会用到FragmentActivity下嵌套多个Fragment,但是在开发过程中会发现在嵌套的Fragment中使用onActivityResult回调方法没有被执行 ...

  3. Android 在 Fragment 中使用 getActivity() NullPointException 的思考和解决办法

    问题: 使用 AS 在 Fragment 中调用 getActivity() 方法的时候会出现可能为空指针的提醒 使用 monkey 多次十万次测试,会出现 getActivity() NullPoi ...

  4. Android的Fragment中onActivityResult不被调用的解决方案

    常见的,我们会在FragmentActivity中嵌套一层Fragment使用,甚至两次或多层Fragment嵌套使用.这个时候,在第二级或者更深级别的Fragment调用startActivityF ...

  5. Android的Fragment中onActivityResult不被调用

    1.检查该Fragment所属的Activity中,是否重写了onActivityResult方法. 2.检查Fragment中的startActivityForResult的调用方式. 请确保不要使 ...

  6. Android 在fragment中实现返回键单击提醒 双击退出

    尝试用mvp架构加dagger2来重写了一下,大致功能都实现了,还没有全部完成. 项目地址 接近完成的时候,想在天气信息页面实现一个很常见的功能,也就是点击屏幕下方的返回键的时候不是返回到上一个act ...

  7. Android 在Fragment中修改Activity中的控件

    在当前的Fragment中调用getActivity方法获取依附着的那个Activity,然后再用获取到的Activity去findViewById拿到你需要的控件对其操作就行了.

  8. Fragment中启动一个新的Activity

    最近遇到一个小问题,就是我在主界面中用的是Fragment,其中四个Fragment,然后打算在其中一个里边,写一个TextView(准确地说是Linearout)的单击事件,然后跳转到另外一个Act ...

  9. android开发—Fragment中onCreateView()和onActivityCreated()的区别

    在编写Fragment时,在onCreateView()方法中启动了一个初始化自定义View的方法 initView(),但是调试时就崩溃,打印日志发现是这里出了问题,就将这个方法放到了onActiv ...

随机推荐

  1. C#删除文件夹以及删除文件

    public static void DelectDir(string srcPath) { try { DirectoryInfo dir = new DirectoryInfo(srcPath); ...

  2. mybatis中查询使用#{}和${}的区别

    ${}中的变量什么值,就会简单的替代变量,不会做处理 比如delete * from tb_label where name=${labelname} 如果labelname的值是 something ...

  3. map与forEach区别

    1.  forEach()返回的是undefined 不可以链式调用 return没有用 2. map()返回一个新数组 原数组不会改 3. 没办法终止或者跳过forEach()和map循环  除非抛 ...

  4. PHP开发——变量

    变量的概念 l  变量是临时存储数据的容器: l  变量是存储内存当中: l  我们现实中有很多数据:姓名.性别.年龄.学历等: l  在计算机中,用变量来代替一个一个的数据: l  我们可以把计算机 ...

  5. py2和py3的区别总结

    1.编码 python2默认编码方式ASCII码(不能识别中文,要在文件头部加上  #-*- encoding:utf-8 -*-  指定编码方式) python3默认编码方式unicode(可识别中 ...

  6. 《JAVA程序设计》第四周总结

    第四周作业总结 学习内容: 1.根据教材视频学习第五章:子类和继承 2.调试代码和解决问题 3.上周错题 4.代码托管 知识总结 子类:在类的声明中,通过使用关键字extends来定义一个类的子类. ...

  7. vscode快捷键的中文版

    自己整理了一份vscode快捷键的中文版本

  8. leveldb 学习记录(一) skiplist

    leveldb LevelDb是一个持久化存储的KV系统,并非完全将数据放置于内存中,部分数据也会存储到磁盘上. 想了解这个由谷歌大神编写的经典项目. 可以从数据结构以及数据结构的处理下手,也可以从示 ...

  9. 第二阶段第二次spring会议

    昨天我对39个组发表了建议以及总结了改进意见和改进方案. 今天我对便签加上了清空回收站功能 private void 清空回收站ToolStripMenuItem_Click(object sende ...

  10. Java集合:HashSet的源码分析

    Java集合---HashSet的源码分析   一.  HashSet概述: HashSet实现Set接口,由哈希表(实际上是一个HashMap实例)支持.它不保证set 的迭代顺序:特别是它不保证该 ...