我正在尝试找出在 Kotlin 中进行 Android View 绑定(bind)的最佳方法。似乎有几个选项:
findViewById
val button: Button by lazy { findViewById<Button>(R.id.button) }
butterknife
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 中每种 View 绑定(bind)方法的优缺点是什么?
Kotlin Android Extensions 是否与 RecyclerView + ViewHolder 模式配合得很好?
Kotlin Android Extensions 如何通过 include 处理嵌套 View 的 View 绑定(bind)?
ex:对于使用 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 中有很多方法可以访问 View 。快速概览:
我的建议是:
另请参阅:https://www.youtube.com/watch?v=Qxj2eBmXLHg
有趣的是,Jake Wharton(ButterKnife 的原作者)现已加入 Google 并致力于 ViewBinding。
关于android - Kotlin Android View 绑定(bind) : findViewById vs Butterknife vs Kotlin Android Extension,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46482018/