Reputation: 852
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 the highlighting does not go away with the item removed, instead it is applied on the remaining items.
I have tried to remove the items from the model and notify the adapter on the each removal like below
for (item in selectedItems) {
deleteResidentItem(item.key, sharedPreferences.getString("AuthToken", SHARED_PREFERENCE_DOES_NOT_EXIST))
removeSelectedItemsFromModelById(item.key)
residentRecyclerViewAdapter.notifyItemRemoved(item.value)
residentRecyclerViewAdapter.notifyItemRangeChanged(item.value, mutableResidentList.size)
}
even after removing the item the highlighted items still appear on the list randomly, like the example below
i want the highlighted views to be gone after removing from model, how to solve this problem?
Adapter implementation code
class ResidentRecyclerViewAdapter( val residentItems: List<ResidentListModel>):RecyclerView.Adapter<ResidentRecyclerViewAdapter.ResidentViewHolder>() {
lateinit var residentRecyclerViewListenerInterface:ResidentRecyclerViewListenerInterface
fun initResidentRecyclerViewListenerInterface(listener:ResidentRecyclerViewListenerInterface) {
residentRecyclerViewListenerInterface = listener
}
override fun onCreateViewHolder(parent: ViewGroup, p1: Int): ResidentViewHolder {
var layoutInflater = LayoutInflater.from(parent.context)
var inflatedLayout = layoutInflater.inflate(R.layout.single_resident_item, parent, false)
return ResidentViewHolder(inflatedLayout, residentRecyclerViewListenerInterface)
}
override fun getItemCount() = residentItems.size
override fun onBindViewHolder(holder: ResidentViewHolder, posistion:Int) {
//place to bind the values
val ModelName = residentItems.get(posistion).ModelName
val VechicleNo = residentItems.get(posistion).VehicleNo
val SlNo = residentItems.get(posistion).SlNo
holder.vehicle_number.text = VechicleNo
holder.model_name.text = ModelName
holder.sl_no.text = SlNo.toString()
holder.resident_item_wrapper.setOnLongClickListener {
when (holder.is_selected.isChecked) {
// not checked, then check, add to list
false-> {
holder.resident_item_wrapper.setBackgroundColor(Color.parseColor("#1565c0"))
holder.resident_vehicle_number.setTextColor(Color.WHITE)
holder.resident_model_name.setTextColor(Color.WHITE)
holder.is_selected.setBackgroundColor(Color.WHITE)
holder.is_selected.visibility = View.VISIBLE
holder.is_selected.isChecked = true
residentRecyclerViewListenerInterface.onResidentItemLongClickListener(holder.sl_no.text.toString().toInt(), posistion)
}
true -> {
// checked then unselect and remove from list
holder.resident_item_wrapper.setBackgroundColor(Color.WHITE)
holder.resident_vehicle_number.setTextColor(Color.BLACK)
holder.resident_model_name.setTextColor(Color.BLACK)
holder.is_selected.setBackgroundColor(Color.BLACK)
holder.is_selected.visibility = View.GONE
holder.is_selected.isChecked = false
residentRecyclerViewListenerInterface.onResidentItemLongUnselectClickListener(holder.sl_no.text.toString().toInt(),posistion)
}
}
true
}
}
interface ResidentRecyclerViewListenerInterface {
fun onResidentItemLongClickListener(Id:Int, Position:Int)
fun onResidentItemClickListener(Id:Int, Position: Int)
fun onResidentItemLongUnselectClickListener(Id: Int, Position: Int)
}
class ResidentViewHolder(val view: View, residentRecyclerViewListenerInterface:ResidentRecyclerViewListenerInterface):RecyclerView.ViewHolder(view) {
var vehicle_number:TextView
var model_name:TextView
var sl_no:TextView
var resident_item_wrapper:CardView
var is_selected:CheckBox
var resident_vehicle_number:TextView
var resident_model_name:TextView
lateinit var residentRecyclerViewListenerInterface:ResidentRecyclerViewListenerInterface
init {
vehicle_number = view.resident_vehicle_number
model_name = view.resident_model_name
sl_no = view.resident_slno
resident_item_wrapper = view.resident_item_wrapper
is_selected = view.is_selected
resident_vehicle_number = view.resident_vehicle_number
resident_model_name = view.resident_model_name
}
}
}
Upvotes: 3
Views: 2376
Reputation: 69709
Try this:
Take a Boolean
variable in your POJO class
public class POJO {
boolean isSelected;
public boolean isSelected() {
return isSelected;
}
public void setSelected(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(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View view) {
// set the status of selection
pojoArrayList.get(position).setSelected(true);
return true;
}
});
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
}
Upvotes: 2
Reputation: 6813
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)
Upvotes: 2