在 Kotlin 中使用多个参数绑定适配器

Binding adapter with multiple arguments in Kotlin

要为数据绑定适配器使用多个参数,Java 语法是

@BindingAdapter(value={"arg1", "arg2"}, requireAll = false)

然而,这不会在 Kotlin 中编译:

Error:(13, 37) Unexpected tokens (use ';' to separate expressions on the same line)

Kotlin 中多参数的正确语法是什么?

应该是:

@BindingAdapter(value=*arrayOf("arg1", "arg2"), requireAll = false)

请参考官方注释docs for Java Annotations in Kotlin

引用:

For other arguments that have an array type, you need to use arrayOf explicitly:

// Java
public @interface AnnWithArrayMethod {
    String[] names();
}


// Kotlin
@AnnWithArrayMethod(names = arrayOf("abc", "foo", "bar")) class C

编辑:归功于@Francesc

或者你可以简单地这样做

@BindingAdapter("arg1", "agr2", "agr3", "agr4", requireAll = false)

Android Official Docs

中所指

你也可以这样操作:

@BindingAdapter(value= ["deckBackgroundAsFirstParameter", "typeAsSecondParameter"], requireAll = false) 
fun loadBackgroundMethodNameForExample(imageViewForExample: ImageView, deckBackgroundAsFirstParameter: Int?, typeAsSecondParameter: Int?) {
   ...
}

其中 value 是您要使用的参数数组。

从 Kotlin 1.2 开始你可以做到

@BindingAdapter(value = ["arg1", "arg2"], requireAll = false)