Kotlin Extension Functions Databinding

我们两清 提交于 2020-01-12 06:31:50

问题


Is there any possibility to use extension function with a databinding? XML:

<data>
    <import type="my.package.domain.country.model.City.streetName" />

    <variable
        name="city"
        type="my.package.domain.country.model.City" />
</data>

<TextView
    android:id="@+id/city"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@{city.street.streetName()}" />

my.package.domain.country.model.city

data class City(
        val id: String,
        val street: Street
) 

fun City.streetName(): String = street.houseNumber

Error

[kapt] An exception occurred: android.databinding.tool.util.LoggedErrorException: Found data binding errors. ****/ data binding error ****msg:cannot find method streetName() in class my.package.domain.country.model.City

Thanks ;)


回答1:


You have to import CityKt firstly into xml

<import type="my.package.domain.country.model.CityKt" />

int the data section then you can use it like this

<TextView
  android:id="@+id/city"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:text="@{CityKt.streetName(city)}" />

If you review CityKt you will see that there is static Java method with City as first argument



来源:https://stackoverflow.com/questions/49898669/kotlin-extension-functions-databinding

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!