Kotlin扩展功能数据绑定 [英] Kotlin Extension Functions Databinding

查看:93
本文介绍了Kotlin扩展功能数据绑定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以将扩展功能与数据绑定一起使用? XML:

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

my.package.domain.country.model.city

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

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

错误

[kapt]发生异常: android.databinding.tool.util.LoggedErrorException:找到了数据绑定 错误. ****/数据绑定错误**** msg:在类my.package.domain.country.model.City中找不到方法streetName()

[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

谢谢;)

推荐答案

您必须首先将CityKt导入xml

You have to import CityKt firstly into xml

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

int数据部分 那么你可以像这样使用它

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

如果查看CityKt,您将看到存在以City为第一个参数的静态Java方法

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

这篇关于Kotlin扩展功能数据绑定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆