前言: 通过回调接口的方法实现fragment和Activity之间的通信。
效果演示:
布局文件
activity_container.xml
<?xml version="1.0" encoding="utf-8"?> <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" tools:context=".ContainerActivity" android:orientation="vertical" > <TextView android:id="@+id/tv_container" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="hello" android:textColor="@color/black" android:textSize="20sp" android:gravity="center" /> <FrameLayout android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/fl_container" /> </LinearLayout>
fragment.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_change" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="更改Activity中的字体" /> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:text="这是一个Fragment" /> </LinearLayout>
布局页面都很简单基础
接下来是主要的实现代码
1.先在Activity中添加Fragment 在ContainerActivity中实现:
public class ContainerActivity extends AppCompatActivity implements FirstFragment.ChangeListener { private FrameLayout fl_container; private TextView tv_container; private FirstFragment firstFragment; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_container); fl_container = findViewById(R.id.fl_container); tv_container = findViewById(R.id.tv_container); //实例化Fragment firstFragment = new FirstFragment(); //将Fragment添加到Activity中 使用commitAllowingStateLoss容错率更高 建议使用这个 getSupportFragmentManager().beginTransaction().add(R.id.fl_container,firstFragment).commitAllowingStateLoss(); } @Override public void setData(String text) { tv_container.setText(text); } }
之后是FirstFragment
public class FirstFragment extends Fragment { private Button btn_change; private ChangeListener listener; //构建Fragment中的视图 @Nullable @Override public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { View view = inflater.inflate(R.layout.fragment,container,false); return view; } //视图创建完成之后 在这个方法面里面做一些初始化的工作 @Override public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) { super.onViewCreated(view, savedInstanceState); btn_change = view.findViewById(R.id.btn_change); btn_change.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { listener.setData("你好"); } }); } //创建一个接口 public interface ChangeListener{ void setData(String text); } //当Fragment和Activity建立关联的时候调用 @Override public void onAttach(@NonNull Context context) { super.onAttach(context); try { listener= (ChangeListener) context; } catch (Exception e) { e.printStackTrace(); } } }