问题描述
新的 ViewBinding 与具有综合视图绑定的Kotlin Android扩展?
除了新ViewBindings提供的NullSafety和TypeSafety之外,为什么我们应该考虑放弃Kotlin在Views上使用合成绑定的方式.
Apart form the NullSafety and TypeSafety provided by new ViewBindings, why should we consider ditching the Kotlin way of using synthetic bindings on Views.
由于新的ViewBinding可以在事前生成Binding类,因此它的性能更高吗?
Is the new ViewBinding more performat since it generates the Binding class before hand?
推荐答案
让我们回顾一下两者.
- 导入适当的布局综合扩展名:
import kotlinx.android.synthetic.main.<layout>.*
- 通过ID:
textView.text = "Hello, world!"
引用代码中的视图.这些扩展适用于:Activities
,Fragments
和Views
.
- Import appropriate layout synthetic extensions:
import kotlinx.android.synthetic.main.<layout>.*
- Reference views in code via their ids:
textView.text = "Hello, world!"
. These extensions work on:Activities
,Fragments
andViews
.
视图绑定
- 在类内部创建绑定引用:
private lateinit var bindingYourClassBinding
- 在
Activity
的onCreate
内填充绑定binding = YourClassBinding.inflate(layoutInflater)
并调用setContentView(binding.root)
,或在Fragment
的onCreateView
中填充它,然后返回:return binding.root
- 通过使用其ID
binding.textView.text = "Hello, world!"
进行绑定的代码引用视图
- Create binding reference inside your class:
private lateinit var bindingYourClassBinding
- Inflate your binding
binding = YourClassBinding.inflate(layoutInflater)
insideActivity
'sonCreate
and callsetContentView(binding.root)
, or inflate it inFragment
'sonCreateView
then return it:return binding.root
- Reference views in code via binding using their ids
binding.textView.text = "Hello, world!"
类型安全
根据定义,Kotlin Android扩展和 ViewBinding 是类型安全的,因为已将引用的视图强制转换为适当的类型.
Type safety
Kotlin Android Extensions and ViewBinding are type safe by definition, because referenced views are already casted to appropriate types.
Kotlin Android扩展和 ViewBinding 都是null安全的. ViewBinding在这里没有任何优势.对于 KAE ,如果仅在某些布局配置中显示视图,IDE会为您指出:
Kotlin Android Extensions and ViewBinding are both null safe. ViewBinding doesn't have any advantage here. In case of KAE, if view is present only in some layout configurations, IDE will point that out for you:
因此,您只需将其与Kotlin中的其他任何可为null的类型一样对待,错误就会消失:
So you just treat it as any other nullable type in Kotlin, and the error will disappear:
对于 Kotlin Android扩展,布局更改会立即转换为合成扩展的生成,因此您可以立即使用它们.如果是 ViewBinding ,则必须构建您的项目
In case of Kotlin Android Extensions, layout changes instantly translate to generation of synthetic extensions, so you can use them right away. In case of ViewBinding, you have to build your project
对于 Kotlin Android扩展,可能会导入不正确的布局合成扩展,从而导致NullPointerException
.这同样适用于 ViewBinding ,因为我们可以导入错误的Binding
类.虽然是比不正确的类名更容易忽略不正确的导入,尤其是在布局文件以Activity
/Fragment
/View
命名的情况下,尤其如此,因此 ViewBinding 在这里占据上风.
In case of Kotlin Android Extensions, it is possible to import incorrect layout synthetic extensions, thus causing NullPointerException
. The same applies to ViewBinding, since we can import wrong Binding
class. Although, it ismore probable to overlook incorrect import than incorrect class name, especially if layout file is well named after Activity
/Fragment
/View
, so ViewBinding has upper hand here.
- 键入安全性-抽奖.
- 无效安全-抽奖.
- 样板代码- KAE 获胜.来自Kotlin Android扩展文档:
- Type safety - Draw.
- Null safety - Draw.
- Boilerplate code - KAE wins. From Kotlin Android Extensions documentation:
- 应用布局更改- KAE 获胜.与 ViewBinding 相比,更改是即时的.
- 布局使用不正确- ViewBinding 胜出
- Applying layout changes - KAE wins. Changes are instant in contrast to ViewBinding.
- Incorrect layout usage - ViewBinding wins
我认为对于 ViewBinding 替代 KAE 存在很大的误解.人们会听到较大的关键字,并在没有事先验证的情况下重复它们.当然, ViewBinding 是目前Java开发的最佳选择(替代 ButterKnife ),但是在Kotlin中,与 KAE 相比,优势不大或几乎没有优势(请参阅不正确的布局用法部分.
I think there is big misconception about ViewBinding being replacement for KAE. People hear big keywords and repeat them without verifying it beforehand. Sure, ViewBinding is best option for Java development right now (replacement for ButterKnife), but there is no or little advantage over KAE in Kotlin (see Incorrect layout usage section).
旁注:我确定DataBinding的人们会喜欢ViewBinding的:)
Side note:I'm sure DataBinding people will like ViewBinding :)
这篇关于具有合成视图的ViewBinding vs Kotlin Android扩展的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!