哪个可以提供更好的性能?
@Bindable
使用:notifyPropertyChanged(fieldName)
其中
Observablefield
在 set 方法中使用 notifyChange()
来通知文件中的所有内容。
private class User extends BaseObservable {
private String firstName;
private String lastName;
@Bindable
public String getFirstName() {
return this.firstName;
}
@Bindable
public String getLastName() {
return this.lastName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
notifyPropertyChanged(BR.firstName);
}
public void setLastName(String lastName) {
this.lastName = lastName;
notifyPropertyChanged(BR.lastName);
}
}
对比
private class User {
public final ObservableField<String> firstName =
new ObservableField<>();
public final ObservableField<String> lastName =
new ObservableField<>();
public void setFirstName(String firstName) {
firstName.set(firstName)
}
public void setLastName(String lastName) {
lastName.set(lastName)
}
}
最佳答案
没关系,他们都很有效率。
我的印象是,当我们调用 Observablefield .set(newValue)
方法时,它会触发 notifyChange()
,从而更新 User
的所有字段> 类,但这是不正确的。它仅在 notifyChange()
属于该 Observablefield
对象而不是 User
对象时更新该字段。
关于Android数据绑定(bind): @Binable vs Observablefield performance,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55738054/