【发布时间】:2018-03-10 22:58:20
【问题描述】:
我正在尝试找出在 Kotlin 中进行 Android 视图绑定的最佳方法。似乎有几个选项:
findViewById
val button: Button by lazy { findViewById<Button>(R.id.button) }
黄油刀
https://github.com/JakeWharton/butterknife
@BindView(R.id.button) lateinit var button: Button
Kotlin Android 扩展
https://kotlinlang.org/docs/tutorials/android-plugin.html
import kotlinx.android.synthetic.main.activity_main.*
我对 java 领域的 findViewById 和 Butterknife 非常熟悉,但是 Kotlin 中每种视图绑定方法的优缺点是什么?
Kotlin Android Extensions 是否与 RecyclerView + ViewHolder 模式配合得很好?
Kotlin Android Extensions 如何通过include 处理嵌套视图的视图绑定?
例如:对于使用 activity_main.xml 的 Activity,如何访问 View custom1?
activity_main.xml
<...>
<include layout="@layout/custom" android:id="@+id/custom" />
</>
custom.xml
<...>
<View android:id="@+id/custom1" ... />
<View android:id="@+id/custom2" ... />
</>
【问题讨论】:
标签: android kotlin findviewbyid butterknife kotlin-android-extensions