【发布时间】:2018-07-03 10:49:15
【问题描述】:
@Bindable
public String getFirstName() {
return this.firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
notifyPropertyChanged(BR.firstName);
}
@Bindable
public String getLastName() {
return this.lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
notifyPropertyChanged(BR.lastName);
}
@Bindable({"firstName", "lastName"})
public void getName() {
return this.firstName + ' ' + this.lastName;
}
以上代码是我从 Google 的示例代码中提取的 - https://developer.android.com/reference/android/databinding/Bindable
并在 XML 中使用它
<TextView
android:id="@+id/first_name"
.....
android:text="@{myViewModel.firstName}" />
<TextView
android:id="@+id/last_name"
.....
android:text="@{myViewModel.lastName}" />
<TextView
android:id="@+id/full_name"
.....
android:text="@{myViewModel.getName()}" />
每当我打电话给myViewModel.setFirstName("Mohammed"); 时,它都会更新视图中的名字,而不是全名。甚至文档都是错误的,不可靠。
与此问题相关的其他帖子无济于事,因为它们中的大多数都涉及非参数化的 Bindables。
根据文档中的这一行
只要 firstName 或 lastName 有更改通知,name 也将被视为脏。这并不意味着 BR.name 会通知 onPropertyChanged(Observable, int),只是包含 name 的绑定表达式会被弄脏和刷新。
我也尝试调用notifyPropertyChanged(BR.name);,但对结果也没有影响。
【问题讨论】:
标签: android android-databinding bindable