android数据绑定代码中的“@get:Bindable”是什么?

Lal*_*era 3 android

我是 android 数据绑定的新手,我正在查看如下代码

 @get:Bindable
    var userIds: MutableList<Long> = mutableListOf()
        private set(value) {
            field = value
            notifyPropertyChanged(BR.userIds)
        }
Run Code Online (Sandbox Code Playgroud)

那么,@get:Bindable这里有什么。是@Bindable@get:Bindable一样吗?

Khe*_*raj 5

@get:Bindable

简单来说,这将@Bindable在 getter 上添加注释 userIds

下面两个是相同的。或者你可以说两种在 getter 上添加注解的方法。

@get:Bindable
    var userIds: MutableList<Long> = mutableListOf()
        private set(value) {
            field = value
            notifyPropertyChanged(BR.userIds)
        }

var userIds: MutableList<Long> = mutableListOf()
    @Bindable get() = _title
    set(value) {
        field = value
        notifyPropertyChanged(BR.userIds)
    }
Run Code Online (Sandbox Code Playgroud)

为了在Java中更清楚地理解,与下面相同。

private ArrayList<Long> userIds = new ArrayList<>();

@Bindable
public ArrayList<Long> getUserIds() {
    return userIds;
}

public void setUserIds(ArrayList<Long> userIds) {
    this.userIds = userIds;
    notifyPropertyChanged(BR.selected);
}
Run Code Online (Sandbox Code Playgroud)

您可以在官方文档中了解更多关于 Annotations 的信息。