Android数据绑定:@Binable vs Observablefield性能

时间:2019-04-18 01:43:24

标签: android android-layout android-databinding

哪个在这里能提供更多的性能?

@Bindable使用:notifyPropertyChanged(fieldName)其中 Observablefield在设置方法中使用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);
   }
}

vs

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

}

1 个答案:

答案 0 :(得分:1)

没关系,它们都是有效的。

给我的印象是,当我们调用Observablefield .set(newValue)方法时,它会触发notifyChange()来更新User类的所有字段,但这是不正确的。它只会更新该字段,因为notifyChange()属于该Observablefield对象,而不是User对象。