Android:DataBinding 和 ViewBinding 的区别 [英] Android : Difference between DataBinding and ViewBinding
问题描述
自 Jetpack 发布以来,我们一直在使用 DataBinding.Android 文档表明 ViewBinding 已添加到 Android Studio 3.6 Canary 11+ 中.
We are using DataBinding since Jetpack release. Android documentation indicates that ViewBinding was added in Android Studio 3.6 Canary 11+.
我阅读了文档,但它看起来类似于 DataBinding.
I read the documentation but its looks similar to DataBinding.
谁能解释一下这两个概念有什么区别?
Can anyone explain what's the difference between these two concepts?
推荐答案
根据官方文档:
仅将视图绑定到代码.
将数据(从代码)绑定到视图 + ViewBinding(将视图绑定到代码)
Binding data (from code) to views + ViewBinding (Binding views to code)
三个重要的区别
使用视图绑定,布局不需要布局标签
With view binding, the layouts do not need a layout tag
您不能使用视图绑定将布局与 xml 中的数据绑定(没有绑定表达式,没有 BindingAdapters 也没有视图绑定的双向绑定)
You can't use viewbinding to bind layouts with data in xml (No binding expressions, no BindingAdapters nor two-way binding with viewbinding)
视图绑定的主要优点是速度和效率.它的构建时间更短,因为它避免了由于注释处理器影响数据绑定的构建时间而导致的与数据绑定相关的开销和性能问题.
The main advantages of viewbinding are speed and efficiency. It has a shorter build time because it avoids the overhead and performance issues associated with databinding due to annotation processors affecting databinding's build time.
简而言之,视图绑定没有什么是数据绑定不能做的(尽管以更长的构建时间为代价),并且有很多数据绑定可以做到而视图绑定不能
In short, there is nothing viewbinding can do that databinding cannot do (though at cost of longer build times) and there are a lot databinding can do that viewbinding can"t
这篇关于Android:DataBinding 和 ViewBinding 的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!