简体   繁体   English

Android绑定 - 属性更改

[英]Android Binding - property changing

I have problem with Android binding library. 我有Android绑定库的问题。 When I'm using property changed '_all' everything works, but when I'm specifying field it don't work. 当我使用属性更改'_all'时一切正常,但是当我指定字段时它不起作用。 My question is why ? 我的问题是为什么? :) :)

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
}

And my xml file: 和我的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>

The problem is that you are using the method getName() instead of the property name . 问题是您使用方法getName()而不是属性name You should bind it like this: 你应该像这样绑定它:

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@{person.name}" />

The reason that _all works is because data binding considers that the entire object is invalid and reevaluated methods then as well. _all起作用的原因是因为数据绑定认为整个对象是无效的,然后也是重新评估的方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM