Android 数据绑定不更新

问题描述 投票:0回答:0

我正在尝试根据我的

CheckBox
是否为
checked
显示一个面板,我想在纯 XML 中执行此操作,因为它只是一个不需要任何特定模型属性的视觉绑定。

我从这个示例代码开始,在我的构建属性中启用数据绑定后,我希望它可以开箱即用。

<layout xmlns:android="http://schemas.android.com/apk/res/android">
    <data>
        <import type="android.view.View"/>
        <variable
            name="isChecked"
            type="boolean" />
    </data>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <CheckBox
            android:id="@+id/checkbox"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:checked="@={isChecked}" />

        <FrameLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="@color/black"
            android:visibility="@{isChecked ? View.VISIBLE : View.GONE}">
            <!-- Your layout content here -->
        </FrameLayout>
    </LinearLayout>
</layout>

相反,我在启动应用程序时看到的是

CheckBox
FrameLayout
,甚至多次单击
CheckBox
也不会改变它。

似乎数据绑定更新从未被触发,所以无论如何我都停留在初始状态。

我错过了什么?这应该是一件直截了当的事情。

作为旁注,即使尝试使用

android:visibility="@{checkbox.checked ? ...}"
android:visibility="@{checkbox.isChecked() ? ...}"
也无济于事。

android android-xml android-databinding android-checkbox
© www.soinside.com 2019 - 2024. All rights reserved.