【发布时间】:2020-06-14 11:00:19
【问题描述】:
我有如下代码
recycler_view.apply {
// Some other code
LinearSnapHelper().attachToRecyclerView(this)
}
如果我想使用apply,下面的this 会出错
recycler_view.apply {
// Some other code
LinearSnapHelper().apply {
.attachToRecyclerView(this) // This will error because `this` is LinearSnapHelper()
}
}
我试过this@RecyclerView还是报错
recycler_view.apply {
// Some other code
LinearSnapHelper().apply {
.attachToRecyclerView(this@RecyclerView) // Still error
}
}
我试过this@recycler_view还是报错
recycler_view.apply {
// Some other code
LinearSnapHelper().apply {
.attachToRecyclerView(this@recycler_view) // Still error
}
}
将this 引用到recycler_view 的语法是什么?
注意:我可以执行以下操作,但只是想学习如何拥有 this 中的 apply 指的是 Kotlin Android Extension 类型类。
recycler_view.apply {
// Some other code
LinearSnapHelper().apply {
// Some other code
}.attachToRecyclerView(this)
}
【问题讨论】:
标签: android kotlin kotlin-android-extensions