androidandroid-recyclerviewadapterandroid-adapterexpandablerecyclerview

RecyclerView notifyItemRangeRemoved no animation


I used recyclerView with notifyItemRangeInserted and notifyItemRangeRemoved.

RecyclerView in my screen used width="match_parent" and height="wrap_content"

I need to show or hide ALL elements in my recylerView. I used this code and it works nice. Element hide and show after change visibility flag.

But for some reason animation for insertion is present and for deletion there is no. Why?

class MyAdapter : RecyclerView.Adapter<ViewHolder>() {

var objects: List<MyData> = emptyList()
    set(value) {
        field = value
        notifyDataSetChanged()
    }

var visibility: Boolean = true
    set(value) {
        field = value

        when (field) {
            true  -> notifyItemRangeInserted(0, cards.size)
            false -> notifyItemRangeRemoved(0, cards.size)
        }
    }

override fun getItemCount(): Int = if (visibility) objects.size else 0

Solution

  • Having researched the Internet, I realized that RecyclerView has problems with animations when it height = wrap_content.

    in some version of the recycler library google this was fixed, but from the answers of people I found that the problem is relevant for androidx library