如何使用 kotlin.Pair 进行数据绑定?

How to use kotlin.Pair with databinding?

只要我使用具有两个字段的数据模型,它就可以正常工作。

但是当我尝试使用 kotlin 时。配对失败并给出 nit 信息性错误:

....\DataBinderMapperImpl.java:20: error: cannot find symbol
import .....databinding.ItemCurrencyBindingImpl;


  symbol:   class ItemCurrencyBindingImpl

  location: package ......databinding

Xml代码:

    <variable
        name="currency"
        type="kotlin.Pair"/>
</data>

<TextView
    android:layout_width="@dimen/currency_dropdown_width"
    android:layout_height="wrap_content"
    android:layout_gravity="center_vertical"
    android:gravity="center|start"
    android:padding="@dimen/currency_dropdown_padding"
    android:text="@{currency.first}"
    app:drawableEnd="@{currency.second}"
    app:drawableSize="@{@dimen/currency_dropdown_image_size}"
    tools:text="@string/pound"/></layout>

您需要将类型对的导入添加到您的数据部分,并声明该对的元素的类型。

如果您 currency 类型是 Pair<String, Int> 您可以这样声明:

<import type="kotlin.Pair"/>
<variable name="currency" type="Pair&lt;String, Int&gt;"/>