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

查看:1595
本文介绍了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;
    }
}

我的视图模型如下所示

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中使用诸如Strings之类的主值.

How can I make sure when some filed 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天全站免登陆