0
votes

I have a RecyclerView and I'm trying to implement delete functionality. In order to get the correct positions of the itemViews, I call notifyItemRangeChanged() after deleting. However when I do that the cool animation of the itemView sliding to the right and being deleted is now gone. It looks like the notifyItemRangeChanged() cuts off the animation created by notifyItemRemoved(). Is there any way I can have the animation and the items delete properly?

Activity Code:

        circuitsObject?.circuits?.remove(circuitsObject?.circuits?.get(position))
        recyclerView.adapter?.notifyItemRemoved(position)
        recyclerView.adapter?.notifyItemRangeChanged(position, circuitsObject?.circuits!!.size)

        if (recyclerView.adapter?.itemCount!! == 0) {
            loadEmptyUI()
        }

Adapter Code:

class CircuitViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
...
itemView.setOnClickListener { clickListener(circuit) }
itemView.setOnLongClickListener {
    onLongClickListener(position)
    true
}

The parameter "position" comes straight from the ViewHolder. To compensate for the fact that the position doesn't match the index of my data I am using "notifyItemRangeChanged". However, doing so gets rid of the animation. So I'm wondering if there's any way to bypass this such that I can still have the proper position logic as well as the animation for delete.

1

1 Answers

1
votes

You don't need to call notifyItemRangeChanged(...) after calling notifyItemRemoved(...).