目前Fragment的传值可以使用Bundle,JetPack使用数据分离的思想,利用ViewModel来达到Fragment之间传值的目的,下面我来讲解下。
这篇文章涉及了Databing,Navigation,ViewModel和LiveData等知识点,初看难度较大,后期补上其他教程。
Fragment1中使用SeekBar修改值,Fragment2中跳转后能查看,反之亦然。
public class GlobalViewModel extends ViewModel {
private MutableLiveData<Integer> mutableLiveData;
public void add(Integer x){
mutableLiveData.setValue(mutableLiveData.getValue() + x);
if (mutableLiveData.getValue() < 0) {
mutableLiveData.setValue(0);
}
}
public MutableLiveData<Integer> getNumber(){
if (mutableLiveData == null) {
mutableLiveData = new MutableLiveData<>();
mutableLiveData.setValue(0);
}
return mutableLiveData;
}
}
注意,获取ViewModel对象时需要传入Activity,数据才能共享。
HomeFragment
public class HomeFragment extends Fragment {
public HomeFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
GlobalViewModel viewModel = ViewModelProviders.of(getActivity()).get(GlobalViewModel.class);
FragmentHomeBinding binding = DataBindingUtil.inflate(inflater, R.layout.fragment_home, container,
false);
binding.setData(viewModel);
binding.setLifecycleOwner(this);
binding.seekBar.setProgress(viewModel.getNumber().getValue());
binding.seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
binding.getData().getNumber().setValue(progress);
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
});
binding.btnGotoDetail.setOnClickListener(v -> {
NavController navController = Navigation.findNavController(v);
navController.navigate(R.id.action_homeFragment_to_detailFragment);
});
return binding.getRoot();
}
}
fragment_home
<layout 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">
<data>
<variable
name="data"
type="com.martin.jetmo.jetpack.viewmodel.GlobalViewModel" />
data>
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".fragment.HomeFragment">
<TextView
android:id="@+id/tvProcessNumber"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@{String.valueOf(data.number)}"
android:textSize="24sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.221" />
<SeekBar
android:id="@+id/seekBar"
android:layout_width="0dp"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/tvProcessNumber"
app:layout_constraintVertical_bias="0.158" />
<Button
android:id="@+id/btnGotoDetail"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="GoTo"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.498"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/tvProcessNumber"
app:layout_constraintVertical_bias="0.332" />
androidx.constraintlayout.widget.ConstraintLayout>
layout>
DetailFragment
public class DetailFragment extends Fragment {
public DetailFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
FragmentDetailBinding binding = DataBindingUtil.inflate(inflater, R.layout.fragment_detail,
container, false);
GlobalViewModel viewModel = ViewModelProviders.of(getActivity()).get(GlobalViewModel.class);
binding.textView2.setText(String.valueOf(viewModel.getNumber().getValue()));
binding.setData(viewModel);
binding.setLifecycleOwner(this);
binding.back.setOnClickListener(v -> {
NavController navController = Navigation.findNavController(v);
navController.navigate(R.id.action_detailFragment_to_homeFragment);
});
return binding.getRoot();
}
}
fragment_detail
<layout 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">
<data>
<variable
name="data"
type="com.martin.jetmo.jetpack.viewmodel.GlobalViewModel" />
data>
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".fragment.DetailFragment">
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@{String.valueOf(data.number)}"
android:textSize="24sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.182" />
<Button
android:id="@+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="@{()->data.add(1)}"
android:text="+"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="@+id/button4"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.538" />
<Button
android:id="@+id/button4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="@{()->data.add(-1)}"
android:text="-"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toEndOf="@+id/button3"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.538" />
<Button
android:id="@+id/back"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="204dp"
android:text="back"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.486"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="1.0" />
androidx.constraintlayout.widget.ConstraintLayout>
layout>