For some reason, nofityItemRemoved
only affects the item that is removed and allows all subsequent item numbers to go out of sync. So you need to follow it with a call to notifyItemRangeChanged
.
fun removeItem(viewHolder: RecyclerView.ViewHolder){
removedPosition = viewHolder.adapterPosition
removedItem = arrayList[removedPosition]
arrayList.removeAt(removedPosition)
notifyItemRemoved(removedPosition)
notifyItemRangeChanged(removedPosition, arrayList.size)
}
This answer on a similar question states that if you do it this way, you get a proper animation of the item being removed, but I haven’t tried it:
fun removeItem(viewHolder: RecyclerView.ViewHolder){
removedPosition = viewHolder.adapterPosition
removedItem = arrayList[removedPosition]
arrayList.removeAt(removedPosition)
notifyItemChanged(removedPosition)
notifyItemRangeRemoved(removedPosition, 1)
}
CLICK HERE to find out more related problems solutions.