如何在android

时间:2017-12-12 10:14:33

标签: 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.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>

这是我的视图模型

class NotificationViewModel(val context: Context, val bus: Bus, val manager: CNotificationManager) : BaseObservable() {
    private var checked: Boolean = false
    fun getToolbarTitle(): String? {
        return manager.getToolbarTitle()
    }

    fun setToolbarTextColor(): Int {
        return ContextCompat.getColor(context, R.color.colorDusk)
    }

    fun isChecked(): Boolean {
        return checked
    }

    fun setChecked(checked: Boolean) {
        this.checked = checked
    }

    fun getText(): String {
        if (isChecked()) {
            return "Notfication On"
        } else {
            return "Notification Off"
        }
        notifyChange()
    }
}

我想在switch中应用数据绑定,这样当我在switch上时,textView应该显示Notification off,当我关闭时,检查值应该设置为true {{1应该显示通知关闭和检查布尔变量应该设置为false。请建议我如何使用数据绑定实现。

2 个答案:

答案 0 :(得分:1)

你需要这样做:

在 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")
}

答案 1 :(得分:0)

android:onCheckedChanged="@={notificationViewmodel.executeOnChnaged()}"
android:isChecked="@={notificationViewmodel.ischecked}"