对象字段更改时的 LiveData 更新 [英] LiveData update on object field change

查看:65
本文介绍了对象字段更改时的 LiveData 更新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将 Android MVVM 架构与 LiveData 结合使用.我有一个这样的对象

I'm using Android MVVM architecture with LiveData. I have an object like this

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;
    }
}

我的视图模型看起来像这样

And my view model looks like this

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 中使用诸如字符串之类的主要值对我来说很重要.

How can I make sure when some field in user object changes observers get notified? BTW it is important to me to keep this data in the separate object and not use primary values like Strings in my ViewModel.

推荐答案

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

I don't think there is any best practice as such recommended by android for this. I would suggest you to use the approach which uses cleaner & less boilerplate code.

如果您将 Android 数据绑定与 LiveData 一起使用,您可以采用以下方法:

If you are using android data binding along with LiveData you can go with the following approach:

你的 POJO 对象看起来像这样

Your POJO object would look something like this

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

So you would be already having a class which notifies whenever its property changes. So you can just make use of this property change callback in your MutableLiveData to notify its observer. You can create a custom MutableLiveData for this

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());

        }
    };


}

然后你需要做的就是在你的视图模型中使用这个 CustomMutableLiveData 而不是 MutableLiveData

Then all you need to do is use this CustomMutableLiveData instead of MutableLiveData in your View Model

public class InfoViewModel extends AndroidViewModel {

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

因此,通过这样做,您可以同时通知 view &LiveData 观察者,对现有代码几乎没有改动.希望有帮助

So by doing this you can notify both view & LiveData observer with little change to existing code. Hope it helps

这篇关于对象字段更改时的 LiveData 更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆