一,首頁布局
<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="laobi.com
.viewpagedemo.MainActivity">
<fragment
android:id="@+id/fragment_left"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="match_parent"/>
<fragment
android:id="@+id/fragment_right"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="match_parent"/>
</LinearLayout>
二,實現(xiàn)方式;
1,方式一:先調(diào)用getFragmentManager()對象,然后調(diào)用findFragmentById()方法獲得目標(biāo)fragment,將數(shù)據(jù)傳遞到目標(biāo)fragment
Fragment fragment_left = getFragmentManager().findFragmentById(R.id.fragment_left);
fragment_left.setTextView(et_frafment.getText().toString().trim())//setTextView()是目標(biāo)fragment自己定義的方法2,方式二: 先調(diào)用getFragmentManager()對象,然后調(diào)用findFragmentById()方法獲得目標(biāo)fragment,再調(diào)用getView()獲得目標(biāo)fragment的view對象,最后調(diào)用view的findViewById()獲得目標(biāo)控件
TextView tv_fragment= (TextView)getFragmentManager()
.findFragmentById(R.id.fragment_left).getView().findViewById(R.id.tv_fragment);3,方式三:先調(diào)用getActivity()獲取所在的activity對象后,然后通過findViewById()找到目標(biāo)控件(該目標(biāo)fragment在activity中,所以可以通過activity進行findViewById)
TextView tv_fragment = (TextView)getActivity().findViewById(R.id.tv_fragment);
tv_fragment.setText(et_frafment.getText().toString().trim());