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

  • 相关阅读:
    将数字转化为字符串
    给定一列数字将其平移n位
    判断回文数的问题
    c语言链表逆序的问题
    python中类属性和实例属性的区别
    python中__repr__()方法
    python中模块和包
    flask如何写一个model
    遍历文件夹下excel文件并且写入一个新excel
    python统计任务耗时
  • 原文地址:https://www.cnblogs.com/luckyplj/p/10689876.html
Copyright © 2020-2023  润新知