为了账号安全,请及时绑定邮箱和手机立即绑定

属性更改时 UI 不更新

属性更改时 UI 不更新

泛舟湖上清波郎朗 2023-10-27 10:48:41
当我从 MainActivity 更新 LoginViewModel 中的 User 属性时,我的 UI 没有改变。(卡在这个问题上 2 天了,找不到解决方案)我的 User 类扩展了 BaseObservable(添加了 @Bindable 和 notificationPropertyChanged()),但我不知道我还应该做什么。这些是课程:用户:public class User extends BaseObservable {    protected String  userName;    protected String lastName;    public User(String userName, String lastName) {        this.userName = userName;        this.lastName = lastName;        notifyPropertyChanged(BR._all);    }    @Bindable    public String getUserName() {        return userName;    }    public void setUserName(String userName) {        this.userName = userName;        notifyPropertyChanged(BR.userName);    }    @Bindable    public String getLastName() {        return lastName;    }    public void setLastName(String lastName) {        this.lastName = lastName;        notifyPropertyChanged(BR.lastName);    }}视图模型:public class LoginViewModel extends ViewModel {    private User user;    public LoginViewModel() {        user = new User("Pero","Peric");    }    public User getUser() {        return user;    }    public void setUser(User user) {        this.user = user;    }    public String getUserName()    {        return user.getUserName();    }    public void setUserName(String userName)    {        user.setUserName(userName);    }    public String getLastName()    {        return user.getLastName();    }    public void setLastName(String lastName)    {        user.setUserName(lastName);    }}
查看完整描述

1 回答

?
蝴蝶刀刀

TA贡献1801条经验 获得超8个赞

问题是我:


a)hadnt implement Observerble interface to ViewModel(I susspect that DataBinding 

    adds/removes his listener through that methods)

b)made get methods @Bindable,and notify notifyPropertyChanged() in seters,

c)add property in ViewModel of type PropertyChangeRegistry

d)add notifyPropertyChanged(int fieldID) method

视图模型:


public class LoginViewModel extends ViewModel implements Observable {

    private User user;

    private PropertyChangeRegistry callbacks;



    public LoginViewModel() {

        user = new User("Karlo","maricevic");

        callbacks = new PropertyChangeRegistry();

    }


    public User getUser() {

        return user;

    }


    public void setUser(User user) {

        this.user = user;

    }


    @Bindable

    public String getUserName()

    {

        return user.getUserName();

    }


    public void setUserName(String userName)

    {

        user.setUserName(userName);

        notifyPropertyChanged(BR.userName);

    }


    @Bindable

    public String getLastName()

    {

        return user.getLastName();

    }



    public void setLastName(String lastName)

    {

        user.setUserName(lastName);

    }


    @Override

    public void addOnPropertyChangedCallback(OnPropertyChangedCallback callback) {

        callbacks.add(callback);

    }


    @Override

    public void removeOnPropertyChangedCallback(OnPropertyChangedCallback callback) {

        //callbacks.remove(callback);

    }


    void notifyPropertyChanged(int fieldId)

    {

        callbacks.notifyCallbacks(this,fieldId,null);

    }

}

PS:在这个例子的小实验之后,你不需要用户扩展 BaseObservable 来从 ViewModel 更新 UI


查看完整回答
反对 回复 2023-10-27
  • 1 回答
  • 0 关注
  • 107 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信