I'm trying to highlight a recyclerview item while adhering to MVVM principles and while using databinding. But I'm having trouble understanding how to handle selecting a row.
I currently pass my recyclerview item onclick to the viewmodel using the following interface:
public interface ItemClickListener
{
void onItemClicked(String id);
}
The view onclick is databound:
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto">
<data>
<variable
name="model"
type="Item" />
<variable
name="listener"
type="ItemClickListener" />
</data>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@{model.name}"
android:onClick="@{() -> listener.onItemClicked(model.name)}" />
</layout>
The viewmodel implements the interface so I have a reference to the Item. The listener is instantiated in the activity and passed into the adapter.
How would I handle selecting a recyclerview item (and giving it a background colour)? Should the viewholder implement onClickListener(View view) and pass it through my interface along with it's position?