1 回答

TA貢獻1801條經(jīng)驗 獲得超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
添加回答
舉報