Android 自定义视图上的两种方式数据绑定。找不到 getter

Android two way data binding on custom View. Cannot find getter

我正在自定义视图上实现两种方式的数据绑定。我遵循了 official android developers 但仍然无法正常工作。我有一个旋钮控制值 属性.

内的整数值
class ControlKnob(context: Context, attributeSet : android.util.AttributeSet) : RelativeLayout(context, attributeSet), IUIControl {
    companion object {
        @JvmStatic
        @BindingAdapter("value")
        fun setValue(knob : ControlKnob, value : Int) {
            if(knob.value != value) {
                knob.value = value
            }

        }

        @JvmStatic
        @InverseBindingAdapter(attribute = "value")
        fun getValue(knob : ControlKnob) : Int {
            return knob.value
        }

        @JvmStatic
        @BindingAdapter("app:valueAttrChanged")
        fun setListeners( knob : ControlKnob, attrChange : InverseBindingListener) {
            knob.setOnProgressChangedListener {
                attrChange.onChange()
            }
        }
    }
    var value : Int = -1
    set(value) {
        field = value
        valueView.text = stringConverter.invoke(value)
    }
....
....
}

内部布局我是这样使用的:

<cz.abc.def.package.controls.ControlKnob
                    android:id="@+id/knob"
                    android:layout_width="100dp"
                    android:layout_height="100dp"
                    android:layout_row="0"
                    android:layout_column="0"
                    app:value="@={viewModel.value}"
                    app:label="Knob" />

我的视图模型:

@Bindable
fun getValue() : Int {
    return someValue
}

fun setValue(value : Int) {
    someValue = value

}

但我还是无法编译它。我得到

Cannot find a getter for cz.abc.def.package.controls.ControlKnob app:value that accepts parameter type 'int'

If a binding adapter provides the getter, check that the adapter is annotated correctly and that the parameter type matches.

这可能是什么原因?

可能是你的监听器绑定适配器有问题?根据 the documentation,事件侦听器 BindingAdapter 值应为 "android:valueAttrChanged",并且您有 "app:valueAttrChanged".

我明白了。事实证明这不是代码的问题。我在 gradle 构建文件中遗漏了 apply plugin: 'kotlin-kapt'。在我将此行添加到模块中的 build.gradle 后,它起作用了。