前言: 使用ViewModel+LiveData实现同一个Activity不同Fragment间的通信。
1、定义ViewModel与LiveData,使用LiveData对progress字段进行包装。
/** * @Author: ly * @Date: 2022/10/24 * @Description: 使用LiveData对progress字段进行包装 */ public class ShareDataViewModel extends ViewModel { private MutableLiveData<Integer> progress; public MutableLiveData<Integer> getProgress() { if (progress == null) { progress = new MutableLiveData<>(); } return progress; } @Override protected void onCleared() { super.onCleared(); progress = null; } }
2、将两个Fragment等比例放置在Activity的布局文件中。
<?xml version="1.0" encoding="utf-8"?> <androidx.constraintlayout.widget.ConstraintLayout 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=".MainActivity"> <fragment android:id="@+id/one_fragment" android:name="com.test.mytest.OneFragment" android:layout_width="match_parent" android:layout_height="0dp" app:layout_constraintBottom_toTopOf="@id/two_fragment" app:layout_constraintTop_toTopOf="parent" /> <fragment android:id="@+id/two_fragment" android:name="com.test.mytest.TwoFragment" android:layout_width="match_parent" android:layout_height="0dp" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintTop_toBottomOf="@id/one_fragment" /> </androidx.constraintlayout.widget.ConstraintLayout>
3、在Fragment的布局文件中放一个SeekBar控件,两个Fragment的布局文件类似
<?xml version="1.0" encoding="utf-8"?> <layout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto"> <data> </data> <androidx.constraintlayout.widget.ConstraintLayout android:layout_width="match_parent" android:layout_height="match_parent"> <TextView android:id="@+id/tv_value" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="fragmentOne" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toRightOf="parent" app:layout_constraintTop_toTopOf="parent" /> <SeekBar android:id="@+id/sb_value" android:layout_width="match_parent" android:layout_height="wrap_content" android:max="100" app:layout_constraintTop_toBottomOf="@id/tv_value" /> </androidx.constraintlayout.widget.ConstraintLayout> </layout>
4、编写Fragment的代码,实现具体的通信这里以OneFragment为例,TwoFragment类似。
/** * @Author: ly * @Date: 2022/10/24 * @Description: OneFragment */ public class OneFragment extends Fragment { private FragmentOneBinding binding; @Nullable @Override public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { binding = DataBindingUtil.inflate(inflater, R.layout.fragment_one, null, false); ShareDataViewModel shareDataViewModel = new ViewModelProvider(this.getActivity()).get(ShareDataViewModel.class); MutableLiveData<Integer> liveData = shareDataViewModel.getProgress(); liveData.observe(this, new Observer<Integer>() { @Override public void onChanged(Integer integer) { binding.sbValue.setProgress(integer); } }); binding.sbValue.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() { @Override public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) { //当用户操作SeekBar时,更新ViewModel中的数据 liveData.setValue(progress); } @Override public void onStartTrackingTouch(SeekBar seekBar) { } @Override public void onStopTrackingTouch(SeekBar seekBar) { } }); return binding.getRoot(); } }
这样当OneFragment中的SeekBar控件滑动时,通过LiveData.setValue()方法,修改了ViewModel中LiveData包装的数据(progress)。由于Fragment通过LiveData.observe()方法监听数据的变化,因此progress字段被修改后,Fragment能够第一时间收到通知并更新UI,这就是利用了ViewModel和LiveData实现Fragment间通信的原理。