0

I have a list of locations listed in a recycler view. When I press the delete icon button, the item of that button should be deleted.

override fun onBindViewHolder(holder: ViewHolder, position: Int) {
            holder.locationTitleTextView.text = locationList[position].name
            holder.coordinatesTextView.text = locationList[position].coordinateText
            holder.deleteLocationButton.setOnClickListener {
                locationList.removeAt(position)
                notifyItemRemoved(position)
                notifyItemRangeChanged(position, locationList.size)
            }
        }

recycler item

Now the code does work in deleting the item, but the animation produced after calling notifyItemRemoved(position) and notifyItemRangeChanged(position, locationList.size) the animation is not as intended.

I'm not sure how to describe the animation I observed, but it went something like this. When I delete any item that is not the last item, the last item becomes invisible and then the whole item list is updated in sec.

Is there any other implementation where the animation of the removal is smooth and not rugged?

Goutham
  • 156
  • 13
  • delete notify item range change. also do not capture position passed to bind viewholder, onclicklistener should reference `getBindingAdapterposition()` instead. – Pawel Sep 01 '23 at 11:54
  • Can you provide a sample code for `getBindingAdapterPosition()` as an answer if possible @Pawel – Goutham Sep 02 '23 at 03:03

1 Answers1

0

Based on this question I would recommend trying this change:

 holder.deleteLocationButton.setOnClickListener {
                locationList.removeAt(position)
                notifyItemRemoved(position)
                notifyItemRangeChanged(position, locationList.size)
                notifyDataSetChanged(); // *** NEW LINE
            }
ryankuck
  • 320
  • 3
  • 15