【发布时间】:2019-12-31 14:15:34
【问题描述】:
更新: 如果我移动到另一个片段并返回到这个片段,则 TextView 会更新...
我无法使用 setValue() 或 postValue() 让 UI 中的 MutableLiveData 更新为新值。我可以通过将 MutableLiveData 更改为 ObservableData 来实现这一点,但重点是使用 LiveData 而不是 ObservableData。
我在其他视图中也有类似的事情,没有问题。不确定这里发生了什么......我唯一能想到的是我在相机活动(通过意图)和这个片段之间来回跳跃。在活动结果中,我将片段中的数据设置为 ViewModel。
我认为我不需要为片段中的值设置任何观察者,因为我在 XML 和 ViewModel 之间有 2 路数据绑定。
my_fragment.XML
<?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"
xmlns:tools="http://schemas.android.com/tools">
<data>
<variable
name="myViewModel"
type="com.example.myapplication.MyViewModel" />
</data>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@={myViewModel.photosCount}"
tools:text="1" />
<Button
android:id="@+id/btnTakePhoto"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Take Picture"
android:onClick="@{myViewModel::navClicked}"/>
</LinearLayout>
</layout>
MyViewModel.java
private MutableLiveData<String> photosCount = new MutableLiveData<>();
private MutableLiveData<Boolean> takePhoto = new MutableLiveData<>();
public MyViewModel() {
photosCount.setValue("0"); // This correctly sets the value to "0" in UI
takePhoto.setValue(true);
}
public MutableLiveData<String> getPhotosCount() {
return photosCount;
}
public MutableLiveData<Boolean> getTakePhoto() {
return takePhoto;
}
public void storeImage() {
....
Log.d(TAG, "Updating UI count to: " + String.valueOf(count)); // this shows the correct value that should be updated in the UI
photosCount.setValue(String.valueOf(count));
Log.d(TAG, "Updated value: " + photosCount.getValue()); // this shows the correct updated value
...
}
public void navClicked(@NonNull View view) {
if (view.getId() == R.id.btnTakePhoto) {
takePhoto.setValue(true);
}
}
现在,由于 LiveData 不保证在更改值时更新 UI,我认为这可能是绑定调度问题。那还没有解决问题...
MyFragment.java
private MyViewModel myViewModel;
MyFragmentBinding binding;
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
super.onCreateView(inflater, container, savedInstanceState);
myViewModel = new ViewModelProvider(this).get(MyViewModel.class);
binding = DataBindingUtil.inflate(inflater, R.layout.my_fragment, container, false);
binding.setMyViewModel(myViewModel);
return binding.getRoot();
}
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
viewModel.getTakePhoto().observe(getViewLifecycleOwner(), takePhoto -> {
if (takePhoto) {
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
if (intent.resolveActivity(getActivity().getPackageManager()) != null) {
File photo = viewModel.createImageFile();
if (photo != null) {
Uri photoURI = FileProvider.getUriForFile(getActivity(),"com.example.fileprovider", photo);
intent.putExtra(MediaStore.EXTRA_OUTPUT, photoURI);
this.startActivityForResult(intent, Constants.RequestCodes.MY_REQUEST_IMAGE_CAPTURE);
}
}
}
});
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == Activity.RESULT_OK) {
if (requestCode == Constants.RequestCodes.MY_REQUEST_IMAGE_CAPTURE) {
viewModel.storeImage();
}
}
}
同样,如果您将 photosCount 从 MutableLiveData 切换到 ObservableData,则问题已得到解决,但这不是 LiveData。
【问题讨论】:
标签: android android-livedata mutablelivedata