数据绑定是否将 UI 逻辑移出了布局?

Does data binding move UI logic out of the layouts?

我对数据绑定还很陌生,刚开始了解 Android 数据绑定库,the documentation 中的一件事让我很烦恼。

Using ViewModel components with the Data Binding Library allows you to move UI logic out of the layouts and into the components, which are easier to test.

在那之后,布局中有这个 - XML:

<CheckBox
    android:id="@+id/rememberMeCheckBox"
    android:checked="@{viewmodel.rememberMe}"
    android:onCheckedChanged="@{() -> viewmodel.rememberMeChanged()}" />

也许只是我的问题,但 onCheckedChanged 属性 在布局 中包含的 "logic" 不是比旧版本多得多吗从 activity 或片段调用 "dumb" 布局上的 setOnCheckedChangeListener() 的时尚方式?这一切似乎有点矛盾。有人可以向我解释数据绑定如何移动 "UI logic out of the layouts" 吗?

您的 ViewModel 现在拥有逻辑,您可以独立于 UI 进行测试,并且 UI 可以使用模拟的 ViewModel 进行测试。它并不总是更少的代码,而是遵循某种模式的更结构化的代码。