How To Clear Highlighted Items In The Recyclerview?
I have implemented the recycler view multiple item selection by changing the background color of item when selected.When i remove those items from the model, items get removed but
Solution 1:
Try this:
Take a Boolean
variable in your POJO class
publicclassPOJO {
boolean isSelected;
publicbooleanisSelected() {
return isSelected;
}
publicvoidsetSelected(boolean selected) {
isSelected = selected;
}
}
make below change in your onBindViewHolder()
method
@Override
public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
if(pojoArrayList.get(position).isSelected()){
// make selection in your item
}else {
//remove selction from you item
}
}
Now inside your onLongClickListener
make your selection true
sample code
sampleButton.setOnLongClickListener(newView.OnLongClickListener() {
@OverridepublicbooleanonLongClick(View view) {
// set the status of selection
pojoArrayList.get(position).setSelected(true);
returntrue;
}
});
And when your want to remove selection use this
pojoArrayList.get(position).setSelected(false);
and when you want to delete item from list use that boolean
variable to delete item
if(pojoArrayList.get(position).isSelected()){
//remove the item from list// and notifyDataSetChanged(); after removing the item from list
}
Solution 2:
Your problem is in when (holder.is_selected.isChecked) {
You should have the information if an item is checked on the ViewModel, not on the View and most definitely not in the ViewHolder.
It should be something like if(residentItems.get(posistion).isSelected){
(Using when
is overkill for binary cases)
Post a Comment for "How To Clear Highlighted Items In The Recyclerview?"