Wednesday 14 July 2021

Nested Recyclerview makes scroll laggy

I am using recyclerview with multi view type and using nested recyclerviews:

enter image description here

But its scrolling very slow. I also handled the child recyclerviews scroll position in onViewRecycled function:

override fun onViewRecycled(holder: RecyclerView.ViewHolder) {
    super.onViewRecycled(holder)
    //save horizontal scroll state
    val key = getSectionID(holder.layoutPosition)

    if (holder is HomeSectionsViewHolder) {
        scrollStates[key] =
            holder
                .itemView
                .findViewById<RecyclerView>(R.id.itemsList)
                .layoutManager?.onSaveInstanceState()
    }
}

And in the onBindViewHolder

val state = scrollStates[key]
        if (state != null) {
            itemBinding.itemsList.layoutManager?.onRestoreInstanceState(state)
        } else {
            itemBinding.itemsList.layoutManager?.scrollToPosition(0)
        }

I also tried:

binding.rvHome.recycledViewPool.setMaxRecycledViews(0, 0);

I am using ListAdapter with DiffUtils. I also tried to set adapter to child recyclerviews in the onCreateViewHolder and submit the list from onBindViewHolder and also tried setting adapter from onBindViewHolder. But in all cases main recycler view recycling the views and lagging. I dont want to disable the recycling of the recyclerview.

Can anyone suggest me what i am doing wrong here.



from Nested Recyclerview makes scroll laggy

No comments:

Post a Comment