【问题标题】:UI not updating when property changes属性更改时 UI 不更新
【发布时间】:2020-02-11 13:26:06
【问题描述】:

当我从 MainActivity 更新我在 LoginViewModel 中的 User 属性时,我的 UI 没有改变。(现在这个问题卡住了 2 天,找不到解决方案)

我的 User 类扩展了 BaseObservable(添加了 @Bindable 和 notifyPropertyChanged()),但我不知道我还应该做什么。

这些是类:

用户:

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

主活动

public class MainActivity extends AppCompatActivity {

    ActivityMainBinding binding;
    LoginViewModel viewModel;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = DataBindingUtil.setContentView(this,R.layout.activity_main);
        viewModel = ViewModelProviders.of(this).get(LoginViewModel.class);
        binding.setViewModel(viewModel);
        binding.executePendingBindings();
        binding.getViewModel().setUserName("John");
        binding.executePendingBindings();
    }
}

XML

 <data>

        <variable
            name="viewModel"
            type="com.example.bindinglearning.viewModels.LoginViewModel" />

    </data>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="horizontal">


            <EditText

                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@={viewModel.userName}"
                >
            </EditText>

            <EditText
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@{viewModel.lastName}"></EditText>

        </LinearLayout>

【问题讨论】:

    标签: java android mvvm data-binding


    【解决方案1】:

    问题是我:

    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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-08-07
      • 1970-01-01
      • 1970-01-01
      • 2017-04-06
      • 2018-05-18
      • 2015-04-06
      相关资源
      最近更新 更多