首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Android绑定-属性更改

Android绑定-属性更改
EN

Stack Overflow用户
提问于 2017-02-09 20:32:17
回答 1查看 98关注 0票数 1

我对Android绑定库有问题。当我使用属性更改的'_all‘时,一切都正常,但当我指定字段时,它就不起作用了。我的问题是为什么?:)

代码语言:javascript
复制
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文件:

代码语言:javascript
复制
<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>
EN

回答 1

Stack Overflow用户

发布于 2017-02-10 12:38:29

问题是您使用的是getName()方法,而不是属性name。你应该这样绑定它:

代码语言:javascript
复制
<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@{person.name}" />

_all之所以有效,是因为数据绑定认为整个对象是无效的,因此也会对方法进行重新计算。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/42136827

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档