0

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
Sky
  • 521
  • 1
  • 5
  • 14
  • Looks like the issue is `wrap_content` height of recyclerview. Upon removing all items recyclerviews height becomes 0 (during next layout pass) so it cannot run any disappearing animations. It doesn't happen during insertion because height is expanded prior to animation so it has no issue running them. – Pawel Mar 18 '21 at 15:02
  • can i try use match_parent? But the problem is that the given list is a nested list in the main list. I have a global list whose item is represented by view + list of additional view (this is the adapter for the nested list) – Sky Mar 19 '21 at 12:34
  • and i think, i can not to use match_parent for inner list ( – Sky Mar 19 '21 at 12:40
  • if this `RecyclerView` is an item/row in an outer `RecyclerView`, why not instead use `notifyItemRemoved()` and `notifyItemInserted()` on the parent list? – CSmith Mar 24 '21 at 11:10
  • because the item/row includes one more view besides the list. And if i do this, I will lose the display of other views that are in the viewholder – Sky Mar 25 '21 at 11:44

1 Answers1

0

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

Sky
  • 521
  • 1
  • 5
  • 14