简体   繁体   English

在Kotlin中绑定具有多个参数的适配器

[英]Binding adapter with multiple arguments in Kotlin

To use multiple arguments for a data binding adapter the Java syntax is 要为数据绑定适配器使用多个参数,请使用Java语法

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

However this does not compile in Kotlin: 但是这不能在Kotlin中编译:

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

what's the correct syntax for multiple arguments in Kotlin? 什么是Kotlin中多个参数的正确语法?

Should be: 应该:

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

Please refer to the official annotations docs for Java Annotations in Kotlin 请参阅Kotlin中Java Annotations的官方注释文档

Quoting: 引用:

For other arguments that have an array type, you need to use arrayOf explicitly: 对于具有数组类型的其他参数,您需要显式使用arrayOf:

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


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

EDIT: Credit to @Francesc 编辑:归功于@Francesc

Or you can just simply do this 或者你可以简单地做到这一点

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

as pointed in Android Official Docs 正如Android官方文档中所指出的那样

从Kotlin 1.2你可以做到

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

You can also do it by this way: 你也可以这样做:

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

where value is an array of the parameters that you want to use. 其中value是要使用的参数数组。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM