如何使“this”在作用域中引用Kotlin Android扩展类型类?

如何使“this”在作用域中引用Kotlin Android扩展类型类?,android,kotlin,kotlin-android-extensions,Android,Kotlin,Kotlin Android Extensions,我有一个代码如下 recycler_view.apply { // Some other code LinearSnapHelper().attachToRecyclerView(this) } 如果我想使用apply,下面是错误信息 recycler_view.apply { // Some other code LinearSnapHelpe

我有一个代码如下

        recycler_view.apply {
            // Some other code
            LinearSnapHelper().attachToRecyclerView(this)   
        }
如果我想使用apply,下面是错误信息

        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
            }
        }
        recycler_view.apply {
            // Some other code
            LinearSnapHelper().apply {
                  .attachToRecyclerView(this@recycler_view) // Still error
            }
        }
我试过了this@recycler_view静止误差

        recycler_view.apply {
            // Some other code
            LinearSnapHelper().apply {
                  .attachToRecyclerView(this@RecyclerView) // Still error
            }
        }
        recycler_view.apply {
            // Some other code
            LinearSnapHelper().apply {
                  .attachToRecyclerView(this@recycler_view) // Still error
            }
        }
引用this to recycler_视图的语法是什么

注意:我可以做下面的事情,但我想学习如何在apply-referendKotlin Android扩展类型类中实现这一点

        recycler_view.apply {
            // Some other code
            LinearSnapHelper().apply {
                // Some other code
            }.attachToRecyclerView(this)
        }

实际上,您可以创建自己的范围,如下所示

recycler_view.apply myCustomScope@ {
      // Some other code
      LinearSnapHelper().apply {
          attachToRecyclerView(this@myCustomScope)
      }
}

实际上,您可以创建自己的范围,如下所示

recycler_view.apply myCustomScope@ {
      // Some other code
      LinearSnapHelper().apply {
          attachToRecyclerView(this@myCustomScope)
      }
}

在这种情况下,可以将显式标签应用于外部lambda:

recycler_view.apply recycler@{
    // Some other code
    LinearSnapHelper().attachToRecyclerView(this@recycler)   
}
然而,嵌套应用块看起来并不是惯用的,可能会让人困惑,我建议对recycler_视图使用其他作用域函数,如let:


在这种情况下,可以将显式标签应用于外部lambda:

recycler_view.apply recycler@{
    // Some other code
    LinearSnapHelper().attachToRecyclerView(this@recycler)   
}
然而,嵌套应用块看起来并不是惯用的,可能会让人困惑,我建议对recycler_视图使用其他作用域函数,如let: