android - lambda kotlin 解决方法中的智能转换变量

标签 android variables lambda casting kotlin

我今天通过了一个 lambda,它有一个未知类型的变量,在 when..is 条件下,变量不能智能转换为 is 条件下的类型......它给出了它是不可能,因为变量是 Public Api,是否有解决方法?

enter image description here

最佳答案

您可以创建一个更方便的 onBind 扩展函数,它具有 itemview 等传递给 lambda 而不是接收一个 ItemViewTypePosition:

inline fun LastAdapter.Builder.onBind(crossinline f: (item: Any, view: View, type: Int, position: Int) -> Unit): LastAdapter.Builder {
    return onBindListener(object : OnBindListener {
        override fun onBind(item: Any, view: View, type: Int, position: Int) {
            f(item, view, type, position)
        }
    })
}

用法:

builder.onBind { item, view, type, position ->
    when (item) {
        is Product -> view.number_sold.text = item.price.toString()
    }
}

关于android - lambda kotlin 解决方法中的智能转换变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40497726/

相关文章:

unicode - g++ unicode 变量名

C++ lambda 捕获约束

c# - 遍历表达式树并提取参数

C# 将 Lambda 表达式作为方法参数传递

android - 使用 Completable.timer 对 RxJava 进行单元测试

Android - 在应用程序中嵌入 youtube iframe

android - 如何检查 SwitchPreference 的当前状态?

android - 可以在 ListFragment 上以编程方式添加 ImageView 吗?

javascript - 基本 "Get Variable Value"使用 GopherJS

php - x 时间后递增 PHP 变量