![](/img/trans.png)
[英]Android data Binding - ObservableArrayMap how to implement in a POJO
[英]how to implement Switch using Data binding in 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>
這是我的視圖模型
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 然后它textview
應該顯示通知關閉並且檢查的布爾變量應該設置為 false。 請建議我如何使用 Data-binding 實現。
你需要這樣做:
內部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")
}
android:onCheckedChanged="@={notificationViewModel.executeOnChanged()}"
android:isChecked="@={notificationViewModel.isChecked}"
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.