Reputation: 2627
We use databinding in out project thus not instantiating controls in code, but doing that within the layout xml. We've done that throughout the app with all UI controls and it works great.
Problem is that I've now added a SwitchCompat
control to my layout. It only has onClick
(not firing) and it doesn't have onCheckedChange
which you would expect from a SwitchCompat, right?
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android">
<data>
<variable
name="viewModel"
type="music.queue_ui.list.QueueListViewModel" />
</data>
<androidx.appcompat.widget.SwitchCompat
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="@{() -> viewModel.toggleHeader()}" />
</layout>
The toggleHeader
never get called :-(
package music.queue_ui.list
import android.widget.Toast
import androidx.lifecycle.ViewModel
import io.reactivex.disposables.CompositeDisposable
class QueueListViewModel(
) : ViewModel() {
private val disposables = CompositeDisposable()
fun toggleHeader() {
// This never gets hit by SwitchCompat `onClick`.
// Why??????
Toast.makeText(
context, "SwitchCompat Clicked",
Toast.LENGTH_LONG
).show()
}
override fun onCleared() {
super.onCleared()
disposables.clear()
}
}
Anyone one knows of a good solution?
Upvotes: 1
Views: 267
Reputation: 3743
Use:
android:onCheckedChanged="@{(v,checked)->viewModel.toggleHeader(checked)}"
Upvotes: 1