我是kotlin的新人.我发现并尝试findViewById
在我的Activity
类中使用合成方法而不是烦人的方法,但我发现"如果我们想在View上调用合成属性(在适配器类中有用),我们还应该导入kotlinx.android.synthetic.main .视图.*." 但我无法弄清楚它究竟是如何起作用的?有什么例子吗?
我正试图找出在Kotlin中进行Android View Binding的最佳方法.似乎有一些选择:
findViewById
val button: Button by lazy { findViewById<Button>(R.id.button) }
Run Code Online (Sandbox Code Playgroud)
牛油刀
https://github.com/JakeWharton/butterknife
@BindView(R.id.button) lateinit var button: Button
Run Code Online (Sandbox Code Playgroud)
Kotlin Android扩展程序
https://kotlinlang.org/docs/tutorials/android-plugin.html
import kotlinx.android.synthetic.main.activity_main.*
Run Code Online (Sandbox Code Playgroud)
我对java版本中的findViewById和Butterknife非常熟悉,但Kotlin中每种视图绑定方法的优缺点是什么?
Kotlin Android扩展程序与RecyclerView + ViewHolder模式相匹配吗?
另外,Kotlin Android Extensions如何处理嵌套视图的视图绑定include
?
例如:对于使用的活动activity_main.xml
,将如何View custom1
访问?
activity_main.xml中
<...>
<include layout="@layout/custom" android:id="@+id/custom" />
</>
Run Code Online (Sandbox Code Playgroud)
custom.xml
<...>
<View android:id="@+id/custom1" ... />
<View android:id="@+id/custom2" ... />
</>
Run Code Online (Sandbox Code Playgroud) android findviewbyid kotlin butterknife kotlin-android-extensions