Android Fragment之间的通信

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.工程效果

Android Fragment之间的通信   Android Fragment之间的通信

效果说明:点击测试tongxunlu按钮,会修改右侧textview中的内容

 

Android Fragment之间的通信

上一篇:Android Studio-Gradle项目中添加JNI生成文件(.so文件)


下一篇:Android 终端性能测试——内存篇