![](/img/trans.png)
[英]Is there a way to pass multiple arguments to a recyclerview adapter in 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)
请参阅Kotlin中Java Annotations的官方注释文档
引用:
对于具有数组类型的其他参数,您需要显式使用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官方文档中所指出的那样
从Kotlin 1.2你可以做到
@BindingAdapter(value = ["arg1", "arg2"], requireAll = false)
你也可以这样做:
@BindingAdapter(value= ["deckBackgroundAsFirstParameter", "typeAsSecondParameter"], requireAll = false)
fun loadBackgroundMethodNameForExample(imageViewForExample: ImageView, deckBackgroundAsFirstParameter: Int?, typeAsSecondParameter: Int?) {
...
}
其中value是要使用的参数数组。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.