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

博观而约取,厚积而薄发。这篇文章主要讲述Android的Fragment中的互相通信-桥梁activity相关的知识,希望能为你提供帮助。
效果图如下:【Android的Fragment中的互相通信-桥梁activity】

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

文章图片

项目结构图如下:
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的内容 publicvoid 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>


    推荐阅读