Android的Fragment中的互相通信-桥梁activity

Posted 城南少年与猫

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Android的Fragment中的互相通信-桥梁activity相关的知识,希望对你有一定的参考价值。

效果图如下:

项目结构图如下:

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>

以上是关于Android的Fragment中的互相通信-桥梁activity的主要内容,如果未能解决你的问题,请参考以下文章

fragment之间的通信

Activity 与 Fragment通信方式-Android

Android初级教程Fragment到Fragment的通信初探

Android EventBus事件总线剖析

android--------实现Activity和Fragment通信的面向对象的万能接口

Android:Fragment与Activity之间的通信方式简单介绍