我对Android绑定库有问题。当我使用属性更改的'_all‘时,一切都正常,但当我指定字段时,它就不起作用了。我的问题是为什么?:)
public class Person extends BaseObservable{
private String name;
@Bindable
public String getName(){
return this.name;
}
//IT WORKS
public void setName(String name){
this.name = name;
notifyPropertyChanged(BR._all); //<- difference
}
//IT DONT WORK
public void setSurname(String name){
this.name = name;
notifyPropertyChanged(BR.name); //<- difference
}和我的xml文件:
<layout xmlns:android="http://schemas.android.com/apk/res/android">
<data>
<variable
name="person"
type="com.myapp.Person" />
</data>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@{person.getName()}" />
</LinearLayout>
</layout>发布于 2017-02-10 12:38:29
问题是您使用的是getName()方法,而不是属性name。你应该这样绑定它:
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@{person.name}" />_all之所以有效,是因为数据绑定认为整个对象是无效的,因此也会对方法进行重新计算。
https://stackoverflow.com/questions/42136827
复制相似问题