android - 数据绑定(bind)是否将 UI 逻辑移出了布局?

标签 android data-binding android-databinding

我是数据绑定(bind)的新手,刚开始了解 Android 数据绑定(bind)库,还有 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 属性在布局中包含的“逻辑” 不是比调用 setOnCheckedChangeListener 的老式方式多得多吗() 在 Activity 或 fragment 的“哑”布局上?这一切似乎有点矛盾。谁能给我解释一下数据绑定(bind)如何“UI 逻辑脱离布局”

最佳答案

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

关于android - 数据绑定(bind)是否将 UI 逻辑移出了布局?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52655120/

相关文章:

android - 如何在Android数据绑定(bind)中使用对象的方法?

java - 我的 Firebase 没有为我的应用程序生成 token

android - 当应用沉浸式模式时出现软键盘时, fragment 中的 EditText 和 Button 上的键盘重叠

c# - 在 MahApps.Metro 中使用数据绑定(bind) ItemsControl 时显示 WindowCommands 的分隔符

android - 对 onClickListeners 使用数据绑定(bind)时如何将 UI 数据传递给 ViewModel 函数?

android - 数据绑定(bind)失败并出现 NoSuchMethodError

android - 如何打开音乐选择器?

android - 是否可以将 Java 字符串常量传递给 Gradle 文件?

c# - 如何在 WPF 中绑定(bind)和刷新 UserControl 绑定(bind)?

wpf - BindingExpression 生成的值对目标无效