android - 对象字段更改的 LiveData 更新

标签 android observable android-livedata android-viewmodel

我正在使用带有 LiveData 的 Android MVVM 架构。我有一个这样的对象

public class User {
    private String firstName;
    private String lastName;

    public String getFirstName() {
        return firstName;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    public String getLastName() {
        return lastName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }
}

我的 View 模型看起来像这样

public class InfoViewModel extends AndroidViewModel {
    MutableLiveData<User> user = new MutableLiveData<>();

    public InfoViewModel(@NonNull Application application) {
        super(application);
        User user = new User();
        user.setFirstName("Alireza");
        user.setLastName("Ahmadi");

        this.user.setValue(user);
    }

    public LiveData<User> getUser(){
        return user;
    }

    public void change(){
        user.getValue().setFirstName(user.getValue().getFirstName() + " A ");
    }
}

如何确保用户对象中的某些字段发生更改时,观察者会收到通知?顺便说一句,将这些数据保存在单独的对象中并且不要在我的 ViewModel 中使用诸如字符串之类的主要值对我来说很重要。

最佳答案

我认为 android 没有为此推荐任何最佳实践。我建议您使用使用更清洁和更少样板代码的方法。

如果您将 android 数据绑定(bind)与 LiveData 一起使用,则可以使用以下方法:

你的 POJO 对象看起来像这样

public class User extends BaseObservable {
    private String firstName;
    private String lastName;

    @Bindable
    public String getFirstName() {
        return firstName;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
        notifyPropertyChanged(BR.firstName);
    }

    @Bindable
    public String getLastName() {
        return lastName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
        notifyPropertyChanged(BR.lastName);
    }
}

因此,您将已经拥有一个类,该类会在其属性更改时发出通知。因此,您可以在 MutableLiveData 中使用此属性更改回调来通知其观察者。您可以为此创建一个自定义的 MutableLiveData

public class CustomMutableLiveData<T extends BaseObservable>
        extends MutableLiveData<T> {


    @Override
    public void setValue(T value) {
        super.setValue(value);

        //listen to property changes
        value.addOnPropertyChangedCallback(callback);
    }

    Observable.OnPropertyChangedCallback callback = new Observable.OnPropertyChangedCallback() {
        @Override
        public void onPropertyChanged(Observable sender, int propertyId) {

            //Trigger LiveData observer on change of any property in object
            setValue(getValue());

        }
    };


}

那么你需要做的就是在你的 View 模型中使用这个 CustomMutableLiveData 而不是 MutableLiveData

public class InfoViewModel extends AndroidViewModel {

    CustomMutableLiveData<User> user = new CustomMutableLiveData<>();
-----
-----

因此,通过这样做,您可以通知 View 和 LiveData 观察者,而无需对现有代码进行任何更改。希望对你有帮助

关于android - 对象字段更改的 LiveData 更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48020377/

相关文章:

java - Appium - 通过java代码在Android设备上滑动无法工作

android - 获取不允许 apk 运行的编译错误

angular - '可观察<对象 >' is not assignable to type ' 可观察<{ 用户 : any; }>'

java - 传递多个参数的可观察更新

android - 无法使用生命周期版本 2.2.0-alpha01 功能

android - Google Play 服务已过时。需要 10298000 但找到 10084470

android - 当用户滚动时,Listview 项目从内存中删除位图

javascript - optionsValue 和可观察数组的 knockout 问题

android - 使用 MediatorLiveData 将多个 LiveData 值合并为一个

viewmodel - Livedata与Rxjava