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

I am implementing two way data binding on custom View. I followed the official android developers but still can't make it work. I have a knob that controlls integer value inside the value property.

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)
    }
....
....
}

Inside layout i use it like this:

<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" />

And my view model:

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

fun setValue(value : Int) {
    someValue = value

}

But still i can't compile it. I get

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.

What could be the cause of this ?

Upvotes: 0

Views: 597

Answers (2)

I figured it out. It turned out that it is not problem with the code. I was missing the apply plugin: 'kotlin-kapt' in the gradle build file. After i added this line into build.gradle in the module it worked.

Upvotes: 1

dominicoder
dominicoder

Reputation: 10165

Maybe your listener binding adapter is wrong? Per the documentation, the event listener BindingAdapter value should be "android:valueAttrChanged" and you have "app:valueAttrChanged".

Upvotes: 0

Related Questions