I'm working on an app which has social media like feed.
Feed consists of regular news like posts (cover image, likes, comments, author, text content etc.)
and
poll like posts - its just like news post but instead of cover image you are presented with several options for a question to choose from.
Example that come to my mind is youtube, there when scrolling you see videos, just pictures with text, and surveys with options sometimes. I'll add screenshot for better understanding.
RV - RecyclerView, VH - ViewHolder
GOAL:
Vertical RV with VH containing dynamic number of vertical views, lets say the number of views ranges from 1 to 10.
IMPLEMENTAION: Put inside VH of outer RV inner vertical RV and submit new list to inner RV adapter inside of onBindViewHolder() of outer RV.
PROBLEM:
Lag on scroll of outer RV caused by inflation of VHs for inner RV.
It's because RV creates VHs on demand as user scrolls and when scroll happens VH for outer RV created which in turn triggers inflation of VHs for inner RV and its the main cause of lag.
The culprit is the inflation for sure because lag occurs only on first scroll of 5-7 VHs and it's gone, which means rebinding is lightweight task.
SOLUTION ATTEMPTS SO FAR:
(Failed) Make inner
RVsuse sharedRecycledViewPool. Because it's needed to inflate about15innerVHsbefore even starting to reuse views in shared pool.(Failed) Experimented with ditching inner
RVcompletely and instead have acustom viewwith10views added inxml layoutand make thengone/visiblebased on required number.=They still need to be inflated, duh..(Semi working) Prepopulate
RecycledViewPoolused by innerRVsthe momentadapterof outerRVgets attached and clear when gets detached. This allowed innerRVsto start usingVHsfrom shared pool right away. The horrible lag seems to be almost completely gone, almost. But it feels really wrong for some reason, plus I have to set my innerVH'sitemViewTypeto-1for it to work.
private val recycledViewPool: RecycledViewPool by lazy {
RecycledViewPool().apply {
setMaxRecycledViews(-1, 30)
}
}
override fun onAttachedToRecyclerView(recyclerView: RecyclerView) {
recycledViewPool.clear()
for (i in 0..30) {
recycledViewPool.putRecycledView(
PollResultOptionVH.create(recyclerView)
)
}
super.onAttachedToRecyclerView(recyclerView)
}
override fun onDetachedFromRecyclerView(recyclerView: RecyclerView) {
super.onDetachedFromRecyclerView(recyclerView)
recycledViewPool.clear()
}
REMAINING PROBLEM:
Although big chunk of lagging is gone there is another problem now - when scrolling outer RV you can feel that its still not completely smooth, as if some operation is till taking longer than 16ms, like micro jitters. As if taking pre inflated views from shared RecycledViewPool is still taking some time and effort. The problem is only on first scroll..
QUESTION:
Can anyone suggest alternative to my solution?
What could be the reason that there is still some jitter?
Any other optimization suggestions, for my outer RV to scroll smoothly?
I know there is solution out there because there are many social media apps with muck more complex VHs that work really smooth, but I couldn't find any.

It might be difficult to diagnose your issue without the actual code. I'll share a few solutions and ideas about what might be happening. I want you to go through these and see if anything helps you figure out your issue.
#1 "Lagging" when scrolling or "lagging" when loading
I'm not sure if you mean that the lagging appears when the RV adapter loads data for the first time or if it appears when you start scrolling. If it appears when you start scrolling, I'd recommend you double-check a few things:
If you have something like the above, I'd recommend you to process the data only once instead of relying on computed properties.
#2 The poll-like view
I think this is most likely what is causing the issue. I'd recommend not using an inner RV no matter what, it might not just cause issues with the scrolling, but you might get issues when the VH gets recycled. If you disable the recycling pool so none of the items actually gets recycled, you are basically using a
LinearLayoutwith that in mind, why don't we just use a custom view? For example:The
PollViewlooks like this:And the XMLs
The
view_holder_poll.xmlThe
view_poll.xmlThe
view_holder_news.xmlAnd the
activity_custom_styling.xmlIt looks like this:
https://i.stack.imgur.com/ejHFF.jpg
Now, notice a few things, I assumed you have a max of 10 options, if that isn't the case, then the only other solution would be to dinamically add the views into the
LinearLayout– if it is possible for you to get a fixed amount of options, even better, that would make the implementation much simpler.But see that the "dynamic" custom view does not cause any lagging. The lagging you are experiencing might also come from other places. Are you loading any images using Glide/Picasso? Do you stop any rendering of those images if the ViewHolder gets scrolled away? Are you loading YouTube videos?
If you have any more info, I'd be happy to drop some more feedback. I hope this is useful.