如何在android中使用数据绑定实现交换机

如何在android中使用数据绑定实现交换机,android,data-binding,kotlin,rx-kotlin,Android,Data Binding,Kotlin,Rx Kotlin,这是我的xml: <data> <variable name="notificationViewmodel" type="com.kdcos.contsync.viewmodel.notification.NotificationViewModel"></variable> </data> <RelativeLayout xmlns:android="http://schemas.android

这是我的xml:

<data>

    <variable
        name="notificationViewmodel"
        type="com.kdcos.contsync.viewmodel.notification.NotificationViewModel"></variable>

</data>


<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/colorpalegrey">

    <RelativeLayout
        android:id="@+id/header"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_gravity="center"
        android:background="#689F38"
        android:gravity="center">

        <include
            android:id="@+id/layout_toolbar"
            layout="@layout/toolbar_white_bg" />
    </RelativeLayout>


    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/header"
        android:layout_marginTop="20dp"
        android:background="@drawable/topbottomborder"
        android:orientation="vertical">


        <RelativeLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="18dp"
            android:paddingBottom="20dp"
            android:paddingLeft="@dimen/margin_20dp">


            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:fontFamily="sans-serif-medium"
                android:lineSpacingExtra="30sp"
                android:text="@={notificationViewmodel.getText()}"
                android:textColor="@color/colorDusk"
                android:textSize="16sp"
                android:textStyle="normal" />

            <Switch
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentRight="true"
                android:paddingRight="10dp"
                android:onCheckedChanged="@={notificationViewmodel.checked}"
                android:textColor="@android:color/black" />

        </RelativeLayout>
    </LinearLayout>


</RelativeLayout>

我想在
开关
中应用数据绑定,这样当我打开
开关
时,
文本视图
应显示Notification off,当我关闭时选中的值应设置为true,然后它
文本视图
应显示Notification off,选中的布尔变量应设置为false。请建议我如何使用数据绑定实现。

您需要这样做:

android:onCheckedChanged="@={notificationViewModel.executeOnChanged()}"
android:isChecked="@={notificationViewModel.isChecked}"
xml内部:

android:onCheckedChanged="@{viewModel::executeOnStatusChanged}"
android:checked="@={viewModel.isChecked()}"
在视图模型内:

@Bindable
val isChecked: MutableLiveData<Boolean> = MutableLiveData()

fun executeOnStatusChanged(switch: CompoundButton, isChecked: Boolean) {
     Timber.d("Status changed")
}
@Bindable
val已检查:MutableLiveData=MutableLiveData()
fun executeOnStatusChanged(开关:CompoundButton,isChecked:Boolean){
d.木材(“状态变更”)
}

尝试可能的重复虽然此代码可以解决问题,但如何以及为什么解决问题将真正有助于提高您的帖子质量,并可能导致更多的投票。请记住,你是在将来回答读者的问题,而不仅仅是现在提问的人。请在回答中添加解释,并说明适用的限制和假设。
@Bindable
val isChecked: MutableLiveData<Boolean> = MutableLiveData()

fun executeOnStatusChanged(switch: CompoundButton, isChecked: Boolean) {
     Timber.d("Status changed")
}