Issue with higher order function as a binding adapter

后端 未结 4 669
野的像风
野的像风 2021-02-07 10:09

I am running into issues trying to take a function as a parameter in a binding adapter using Kotlin/Android databinding. This example code throws e: error: cannot generate

相关标签:
4条回答
  • 2021-02-07 10:32

    The declaration () -> Unit suggests a function which takes no input and returns nothing (Unit is the return type in this statement). Your function should look like this:

    fun testFunction() = {}
    
    0 讨论(0)
  • 2021-02-07 10:35

    In the Event Handling section I came across this line:

    In Listener Bindings, only your return value must match the expected return value of the listener (unless it is expecting void)

    For more about error :

    cannot generate view binders java.lang.StackOverflowError

    read this article. hope it will help you!!

    0 讨论(0)
  • 2021-02-07 10:37

    Put apply plugin: 'kotlin-kapt' in build.gradle

    Then you can create Binding Adapter like

    @JvmStatic
    @BindingAdapter("onDelayedClick")
    fun onDelayedClick(view: View, function: () -> Unit) {
        // TODO: Do something
    }
    

    And XML Like

    <View
       android:id="@+id/test_view"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       app:onDelayedClick="@{viewModel.testFunction}"/>
    

    And VM Like

    val testFunction =  {
        // TODO: Do something
    }
    
    0 讨论(0)
  • 2021-02-07 10:38

    Use function: Runnable instead of function: () -> Unit.

    Android's data-binding compiler generates java code, to which, your kotlin function's signature looks like void testFunction(), as kotlin adapts Unit as void when calling from java.

    On the other hand, () -> Unit looks like kotlin.jvm.functions.Function0 which is a function which takes 0 inputs and returns Unit.INSTANCE.

    As you can see these two function signatures don't match, and that's why the compilation fails.

    0 讨论(0)
提交回复
热议问题