Search code examples
androidandroid-recyclerview

setIsRecyclable(false) in ViewHolder makes item remain in background after deleted


While using RecyclerView, I noticed that single item I deleted still exists in the background. Since I use ListAdapter, there were no chance that the changes are not notified to RecyclerViewAdapter.

After I deleted the option setIsRecyclable(false) set in ViewHolder, the problem is solved. But there are only single item bound to that ViewHolder type, so theoretically isRecyclable option should affect nothing.

Is this a bug? Thanks in advance.

p.s I'm using androidx.recyclerview:recyclerview:1.2.1.

Edit: The situation I went through is simillar to this question.


Solution

  • We were facing same the issue with setIsRecyclable(false).

    According to official documentation

    Calls to setIsRecyclable() should always be paired (one call to setIsRecyclabe(false) should always be matched with a later call to setIsRecyclable(true))

    Correct way of making ViewHolder not recyclable is by setting the value to 0 in recycledViewPool

    This is how we fixed it

    recyclerView.recycledViewPool.setMaxRecycledViews(<ITEM VIEW TYPE>, 0)

    <ITEM VIEW TYPE> = Integer value of your item view type that you return in function

    override fun getItemViewType(position: Int): Int {
        val viewType = list[position].viewType
        return viewType
    }