在更新交换机视图状态视图时更新其他视图的可见性双向数据绑定

问题描述

我已经实现了Switch的Two way data binding

@BindingAdapter("switchCheck")
fun setSwitchMaterial(switchMaterial: SwitchMaterial,isChecked: Boolean) {
    switchMaterial.isChecked = isChecked
}

@InverseBindingAdapter(attribute = "switchCheck",event = "switchCheckAttrChanged")
fun getSwitchMaterial(switchMaterial: SwitchMaterial): Boolean {
    return switchMaterial.isChecked
}

@BindingAdapter("switchCheckAttrChanged")
fun setSwitchMaterialListener(view: SwitchMaterial,listener: InverseBindingListener?) {
    view.setonCheckedchangelistener { _,_ ->
        listener?.onChange()
    }
}
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools">

    <data>

        <import type="android.view.View" />

        <variable
            name="viewmodel"
            type="Myviewmodel" />
    </data>

    <androidx.constraintlayout.widget.ConstraintLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:fitsSystemWindows="true">

                     <com.google.android.material.switchmaterial.SwitchMaterial
                        android:id="@+id/switchLimitDepositAmount"
                        style="@style/Text.WizardHeader"
                        switchCheck="@={viewmodel.viewVisible}"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_marginStart="16dp"
                        android:layout_marginTop="16dp"
                        android:layout_marginEnd="16dp"
                        android:text="@string/limit_deposit_amount"
                        android:theme="@style/SwitchTheme"
                        app:layout_constraintEnd_toEndOf="parent"
                        app:layout_constraintHorizontal_bias="0.0"
                        app:layout_constraintStart_toStartOf="parent"
                        app:layout_constraintTop_toBottomOf="@+id/tvSetDepositRules" />

                       
                     <TextView
                        android:id="@+id/textview"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:visibility="=@{viewmodel.viewVisible ? View.VISIBLE :View.GONE}"
                        app:layout_constraintEnd_toEndOf="parent"
                        app:layout_constraintStart_toStartOf="parent"
                        app:layout_constraintTop_toBottomOf="@+id/textView14"/>



     </androidx.constraintlayout.widget.ConstraintLayout>
</layout>

在这里,我试图通过单击“开关”来更新TextView的可见性。

我无法使其正常工作。

我的主要目的是代表保存在viewmodel中的switch state字段更新其他视图的可见性,但它不适用于双向数据绑定,但是当我尝试实现自定义更改侦听器时,它可以正常工作。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)