I want to implement multi select adapter for my recycler view. I m using Mike Penz's FastAdapter for the same.
I am not sure on how to toggle the selected item view color.
Here is the code snippet, attaching a ClickEventHook<SimpleItem>
to the card view in the item so onClick()
is called when clicked on the card view,
private void setupFastAdapter() {
mFastAdapter = new FastAdapter<>();
// Configure the FastAdapter.
mFastAdapter.withSelectable(true);
mFastAdapter.withMultiSelect(true);
mFastAdapter.withSelectOnLongClick(false);
mFastAdapter.withSelectionListener(new ISelectionListener<SimpleItem>() {
@Override
public void onSelectionChanged(SimpleItem item, boolean selected) {
// Toggle the card and text colors.
if (selected) {
Logger.i("Selected [%s]", item.getText());
} else {
Logger.i("Unselected [%s]", item.getText());
}
}
});
// Click listeners for views inside the item, add an `EventHook` to the `FastAdapter` by
// implementing either a `ClickEventHook`, `LongClickEventHook`, `TouchEventHook`, `CustomEventHook`
mFastAdapter.withEventHook(new ClickEventHook<SimpleItem>() {
private CardView cardView;
private TextView textView;
@Nullable
@Override
public View onBind(@NonNull RecyclerView.ViewHolder viewHolder) {
//return the views on which you want to bind this event
if (viewHolder instanceof SimpleItem.ViewHolder) {
cardView = ((SimpleItem.ViewHolder) viewHolder).mCardView;
textView = ((SimpleItem.ViewHolder) viewHolder).mTextView;
return cardView;
} else {
return null;
}
}
@Override
public void onClick(View v, int position, FastAdapter<SimpleItem> fastAdapter, SimpleItem item) {
//react on the click event
Logger.i("Clicked [%s]", item.getText());
if (cardView.isSelected()) {
cardView.setCardBackgroundColor(getActivity().getResources().getColor(R.color.app_green_dark));
textView.setTextColor(getActivity().getResources().getColor(R.color.app_white));
} else {
cardView.setCardBackgroundColor(getActivity().getResources().getColor(R.color.app_light_blue_50));
textView.setTextColor(getActivity().getResources().getColor(R.color.primary_text));
}
}
});
}
Color of the card view and text color of the text view are not toggling. And I am not sure how it is implemented correctly.
I have observed onSelectionChanged()
method is also not called when the card view in the item is clicked.
Can anyone suggest me an approach to toggle the color of the card view and text on selection and deselection?
Thank you in advance,
Mani