Android自定义视图上的双向数据绑定。找不到getter
我正在自定义视图上实现双向数据绑定。我按照计划做了,但还是做不到。我有一个旋钮,它控制value属性中的整数值Android自定义视图上的双向数据绑定。找不到getter,android,kotlin,android-databinding,Android,Kotlin,Android Databinding,我正在自定义视图上实现双向数据绑定。我按照计划做了,但还是做不到。我有一个旋钮,它控制value属性中的整数值 class ControlKnob(context: Context, attributeSet : android.util.AttributeSet) : RelativeLayout(context, attributeSet), IUIControl { companion object { @JvmStatic @BindingAdap
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" />
但我还是不能编译它。我明白了
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.
这可能是什么原因?可能是您的侦听器绑定适配器出错了?根据,事件侦听器BindingAdapter的值应该是android:valueAttrChanged,并且您已经更改了app:valueAttrChanged。。事实证明,这不是代码的问题。我缺少gradle构建文件中的应用插件:“kotlin kapt”。在我将这一行添加到模块中的build.gradle中后,它工作了。将侦听器绑定适配器更改为android:valueAttrChanged没有任何帮助。还是有同样的错误
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.