Fragment Activity之間通信
FragmentFirst 布局
<TextView
android:id="@+id/text_first"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="FragmentFirst"
... />
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button1"
app:layout_constraintTop_toBottomOf="@+id/text_first"
.../>
MainActivity 布局
<fragment
android:id="@+id/fragment1"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:name="com.jimzjy.test2.FragmentFirst"/>
1.通過接口通信
在FragmentFirst中定義 onButtonClick 接口
public class FragmentFirst extends android.app.Fragment {
Button button1;
onButtonClick mCallback;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_1,container,false);
button1 = view.findViewById(R.id.button1);
button1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mCallback.onButtonClick1();
}
});
return view;
}
@Override
public void onAttach(Context context) {
super.onAttach(context);
if (context != null) {
mCallback = (onButtonClick) context;
}
}
public interface onButtonClick {
void onButtonClick1();
}
}
通過在MainActivity中對 onButtonClick1() 的實現來進行通信
@Override
public void onButtonClick1() {
//do something
}
2.在Fragment中調用Activity的方法
FragmentFirst
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_1,container,false);
button1 = view.findViewById(R.id.button1);
button1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
MainActivity activity = (MainActivity) getActivity();
activity.onButtonClick1();
}
});
return view;
}
Fragment之間通信
改變MainActivity布局,添加一個裝第二個Fragment的容器
<fragment
android:id="@+id/fragment1"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:name="com.jimzjy.test2.FragmentFirst"/>
<FrameLayout
android:id="@+id/right_layout"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1">
</FrameLayout>
MainActivity 中動態(tài)加載 FragmentSecond
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
replaceFragment(new FragmentSecond());
}
private void replaceFragment(Fragment fragment){
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.addToBackStack(null);
fragmentTransaction.replace(R.id.right_layout,fragment,"fragment2");
fragmentTransaction.commit();
}
修改MainActivity中onButtonClicl1()的實現
@Override
public void onButtonClick1() {
FragmentSecond fragmentSecond = (FragmentSecond) getFragmentManager().findFragmentByTag("fragment2");
fragmentSecond.textView.setText("1");
}

1.png
點擊按鈕之后

2.png
還可以在Fragment中直接去取得另一個Fragment的實例來通信,但是盡量不要使用