当用户单击时,我试图突出显示我的recyclerview的一行。
Recyclerview行布局:
<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>
onClickListener在活动中实例化
ItemClickListener listener = item -> viewModel.selectItem(item);
viewmodel将Item的isSelected属性设置为true:
public void selectItem(Item item)
{
item.setSelected(true);
}
但是,这不会更改行的背景。通过代码调试显示出该项目的isSelected属性设置为true。有什么我想念的吗?
答案 0 :(得分:0)
使用BaseObservable
class Item extends BaseObservable {
private boolean isSelected = false;
@Bindable
public boolean getIsSelected() {
return isSelected;
}
public void setSelected(Boolean isSelected) {
this.isSelected = isSelected;
//Probably it'll has other name, i haven't testing it
notifyPropertyChanged(BR.selected);
}
}
它将在没有notfiyDataSetChanged的情况下工作