为什么使用 ViewBinding 而不是 DataBinding?

Why use ViewBinding over DataBinding?

我们可以读到 here ViewBindingDatabinding 之间的差别很小。

  • The data binding library processes only data binding layouts created using the <layout> tag.

  • View binding doesn't support layout variables or layout expressions, so it can't be used to bind layouts with data in XML.

是否还有开发人员应注意的其他差异?为什么要创造差异如此之小的新事物?

有人解释一下

Why create something new with such a small difference?

视图绑定的构建时间比数据绑定的构建时间短得多,因为涉及的工作少得多。对于小型项目,这可能无关紧要。对于大型项目,对构建时间的影响可能很大。

以下是您可能更喜欢视图绑定而不是数据绑定的原因之一:

The data binding library processes only data binding layouts created using the <layout> tag. Data binding layout files are slightly different and start with a root tag of layout followed by a data element and a view root element

这会在打算使用合并时导致问题:

Data binding doesn't support include as a direct child of a merge element. For example, the following layout isn't supported:

<?xml version="1.0" encoding="utf-8"?> 
<layout
xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:bind="http://schemas.android.com/apk/res-auto">    <data>
       <variable name="user" type="com.example.User"/>    </data>    <merge><!-- Doesn't work -->
       <include layout="@layout/name"
           bind:user="@{user}"/>
       <include layout="@layout/contact"
           bind:user="@{user}"/>    </merge> 
</layout>

视图绑定的优点是速度和效率。它的构建时间更短,因为它避免了由于注释处理器影响数据绑定的构建时间而导致的与数据绑定相关的开销和性能问题。