I'm trying to highlight a row of my recyclerview when a user clicks on it.
Recyclerview row layout:
<data>
<variable name="model" type="ItemRow" />
<variable name="listener" type="ItemClickListener" />
</data>
<com.google.android.material.card.MaterialCardView
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@{model.name}"
android:onClick="@{() -> listener.onItemClicked(model)}"
android:background="@{model.isSelected ? @drawable/white : @drawable/blue}"/>
</com.google.android.material.card.MaterialCardView>
The onClickListener is instantiated in the activity:
ItemClickListener listener = item -> viewModel.selectItem(item);
The viewmodel sets the isSelected property of Item to true:
public void selectItem(Item item)
{
item.setSelected(true);
}
However, this doesn't change the row's background. Debugging through the code shows me that the item's isSelected property is set to true. Is there something I'm missing?