在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)
请参阅Kotlin中Java注释的官方注释文档
引用:
对于具有数组types的其他参数,您需要显式使用arrayOf:
// Java public @interface AnnWithArrayMethod { String[] names(); } // Kotlin @AnnWithArrayMethod(names = arrayOf("abc", "foo", "bar")) class C
编辑:信贷到@Francesc
或者你可以简单地做到这一点
@BindingAdapter("arg1", "agr2", "agr3", "agr4", requireAll = false)
正如Android官方文档中指出的那样
你也可以这样做:
@BindingAdapter(value= ["deckBackgroundAsFirstParameter", "typeAsSecondParameter"], requireAll = false) fun loadBackgroundMethodNameForExample(imageViewForExample: ImageView, deckBackgroundAsFirstParameter: Int?, typeAsSecondParameter: Int?) { ... }
其中value是您要使用的参数的数组。
从Kotlin 1.2你可以做到
@BindingAdapter(value = ["arg1", "arg2"], requireAll = false)