Fragment之间的通信
一、布局代码
1.activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/container" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="horizontal" tools:context="com.example.tabfragment.MainActivity" tools:ignore="MergeRootFrame" > <fragment android:id="@+id/fragment1" android:layout_width="0dp" android:layout_weight="1" android:layout_height="match_parent" android:name="com.example.tabfragment.Fragment1" /> <fragment android:id="@+id/fragment2" android:layout_width="0dp" android:layout_weight="1" android:layout_height="match_parent" android:name="com.example.tabfragment.Fragment2" /> </LinearLayout>
2.tab1.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:background="#ff0000" android:orientation="vertical" > <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/btnTest" android:text="修改另一个Fragment中View对象的值"/> </LinearLayout>
3.tab2.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:background="#00ff00" android:orientation="vertical" > <TextView android:id="@+id/tv" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="德国夺得2014年巴西世界杯冠军" android:textSize="30sp" /> </LinearLayout>
二、前台代码
1.MainActivity.java
package com.example.tabfragment; import android.app.Activity; import android.app.ActionBar; import android.app.Fragment; import android.app.FragmentManager; import android.app.FragmentTransaction; import android.os.Bundle; import android.view.LayoutInflater; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.view.ViewGroup; import android.widget.TextView; import android.os.Build; public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } }
2.Fragment1.java
package com.example.tabfragment; import android.app.Fragment; import android.os.Bundle; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.Button; import android.widget.TextView; public class Fragment1 extends Fragment { @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view = inflater.inflate(R.layout.tab1, null); Button btnTest = (Button) view.findViewById(R.id.btnTest); btnTest.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Fragment2 fragment = (Fragment2) getActivity().getFragmentManager().findFragmentById(R.id.fragment2); fragment.setTvText(String.valueOf(System.currentTimeMillis())); } }); return view; } }
3.Fragment2.java
package com.example.tabfragment; 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 { private TextView tv; @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view = inflater.inflate(R.layout.tab2, null); tv = (TextView) view.findViewById(R.id.tv); return view; } public void setTvText(String text) { tv.setText(text); } }
效果截图