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 中动态加载 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");
}
点击按钮之后
还可以在Fragment中直接去取得另一个Fragment的实例来通信,但是尽量不要使用