代码之家  ›  专栏  ›  技术社区  ›  Mohammed Atif

无法通知可绑定文件

  •  2
  • Mohammed Atif  · 技术社区  · 7 年前
    @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; 
    }
    

    上面的代码是我从谷歌的示例代码中提取的- 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"); 它正在更新视图中的名字,但不更新全名。即使是这些文件也不可靠。

    与这个问题相关的其他文章没有多大帮助,因为它们大多数都处理非参数化的可绑定文件。

    根据文件中的这一行

    每当firstname或firstname有更改通知时,名称也将被视为脏的。这并不意味着将为br.name通知onpropertychanged(observable,int),只会对包含名称的绑定表达式进行dirtied和refreshed。

    我也试过打电话 notifyPropertyChanged(BR.name); 但对结果也没有影响。

    2 回复  |  直到 7 年前
        1
  •  2
  •   Khemraj Sharma    7 年前

    只是一个黑客

    public class Modal {
        private String firstName;
        private String lastName;
        private String name;
    
        @Bindable
        public String getFirstName() {
            return this.firstName;
        }
    
        public void setFirstName(String firstName) {
            this.firstName = firstName;
            notifyPropertyChanged(BR.firstName);
            notifyPropertyChanged(BR.name);
        }
    
        @Bindable
        public String getLastName() {
            return this.lastName;
        }
    
        public void setLastName(String lastName) {
            this.lastName = lastName;
            notifyPropertyChanged(BR.lastName);
            notifyPropertyChanged(BR.name);
        }
    
    
        @Bindable
        public void getName() {
            return this.firstName + ' ' + this.lastName;
        }
    }
    
        2
  •  1
  •   Mohammed Atif    7 年前

    因此,在深入分析数据绑定概念之后,我发现当我们调用 notifyPropertyChanged BaseObservable 类,它实际上通知属性,但不通知getter和setter。

    因此,在上面的问题中,Java部分没有变化,但是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.name}" />
    

    自从我宣布 getName() 作为 Bindable({"firstName", "lastName"}) ,数据绑定生成属性 name 所以我得听 myViewModel.name 而不是 myViewModel.getName() 在我的XML中。我们甚至不需要通知 名称 为了改变,只通知 firstName lastName 会通知酒店 名称 因为参数化的可绑定。

    但要确保

    推荐文章