Pass class type in databinding attribute

Viewed 214

Can we do something like this in android databinding

itemType="@{MyClass::class}"

For binding adapter

@BindingAdapter(value = ["itemType"])
fun <T> func(
view: View,
itemType: Class<T>,
) {}
1 Answers

No, you wouldn't pass class, rather you would pass data object. Here is the example:

<ListView
            android:id="@+id/bookList"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:items="@{viewmodel.items}" />

And here is the Binding Adapter for this:

@BindingAdapter("app:items")
@JvmStatic
fun setItems(listView: ListView, items: List<Book>) {
    with(listView.adapter as BookListAdapter) {
        replaceData(items)
    }
}

Given that viewmodel.items is LiveData, updating it will automatically update listview.

Related