010 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中的内容

 

posted @   雨后观山色  阅读(435)  评论(0编辑  收藏  举报
编辑推荐:
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
阅读排行:
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· 上周热点回顾(2.24-3.2)
点击右上角即可分享
微信分享提示