@Bindable 不在 BR class 中生成字段

@Bindable doesn't generate a field in the BR class

我需要在字段更改时得到通知。 根据 android 文档,Bindable 注释将在 BR class 中生成一个字段。 (https://developer.android.com/reference/android/databinding/Bindable) 但是在为字段分配 @Bindable 注释后,我在 BR class.

中看不到为该字段创建的字段

我清理并重建项目。但没有优势。 即使我使缓存无效并重新启动。但什么都没有

这是我的javaclass

class Job : BaseObservable(), Serializable {

    @SerializedName("id")
    var id: Int = 0

    @SerializedName("title")
    var title: String = ""

    @SerializedName("is_requested")
    var isRequested: Boolean = false
        @Bindable get
        set(value) {
            field = value
            notifyPropertyChanged(BR.requested)
        }

}

但是我有错误。 BR.requested 未知...

这是自动生成的BRclass先生

public class BR {
  public static final int _all = 0;

  public static final int company = 1;

  public static final int jobExperience = 2;

  public static final int job = 3;

  public static final int educationHistory = 4;

  public static final int user = 5;

  public static final int userLanguage = 6;
}

如您所见,BR class.

中Job.kt的isRequested字段没有对应的字段

替换此

 @SerializedName("is_requested")
  var isRequested: Boolean = false
  @Bindable get

收件人:

 @SerializedName("is_requested")
var isRequested: Boolean
    @Bindable get() = isRequested
    set(value) {
        isRequested = value
        notifyPropertyChanged(BR.isRequested)
    }

无需设置和获取方法。您可以默认访问它。

请注意,您需要应用 kotlin-kapt 插件。

这个答案可能会有帮助