Binding data (from code) to views + ViewBinding (Binding views to code)
There are three important differences
使用视图绑定,布局不需要布局标记
不能使用 viewbind 将布局与 xml 中的数据绑定在一起
(没有绑定表达式,没有 BindingAdapter,也没有带视图绑定的双向绑定)
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.
As you can see, ViewBinding is a sort of subset of DataBinding libraries which means ViewBinding and DataBiding can do the same jobs in terms of binding layouts. And it would also mean with DataBinding, you might not need ViewBinding cause it will do what ViewBinding is supposed to do and also provide a bit of an extra functionalities such as 2way binding, and using variables in XML files.
这就引出了一个问题
"Then let's just use DataBinding as it sounds much more fancy!"
视图绑定允许我们更容易地编写与视图交互的代码。一旦在模块中启用了视图绑定,它将为该模块中存在的每个 XML 布局文件生成一个绑定类。绑定类的实例包含对相应布局中具有 ID 的所有视图的直接引用。
The Data Binding Library is a support library that allows you to bind UI components in your layouts to data sources in your app using a declarative format rather than programmatically.
视图绑定和数据绑定
View Binding library is faster than Data Binding library as it is
not utilising annotation processors underneath, and when it comes to
编译速度视图绑定更有效。