Closed ryandt closed 3 years ago
I know it has been a while since this issue was opened, but I do have a partial solution for this.
It looks like the issue comes from ViewPager2 reusing ViewHolders, as expected since it's using RecyclerView. The original header view is still present, so the library ends up adding another. Easy fix is to remove any pre-existing header, if present, when the layout manager is attached, however that wouldn't cover cases where some pages weren't using StickyHeaders.
Going to spend a bit of time to see if there is a solution to cover every case.
Sorry for the delay, and thank you for the repro project; it made this much easier to debug.
Problem still present. Any updates on this?
Ahh, sorry.
Working on UI tests now to validate the bug and the fix. I don't do great on promising dates, but since I have a fix I'm hopeful for an update soon.
When presenting a list of items with sticky headers within a
Fragment
that's hosted by aViewPager2
, a sticky header bug occurs when moving back to the page from another page in theViewPager2
.Screen recording of bug https://imgur.com/v7XqCFR
Fork of project to demonstrate the bug https://github.com/ryandt/StickyHeaders/tree/view-pager-cache-bug
Steps to reproduce:
While the list is scrolling, "Header at 52" intermittently reappears as a sticky header even when that header's section has been scrolled off the screen.
This issue appears to have something to do with
RecyclerView
's caching strategy. If I callViewPager2.setOffscreenPageLimit(2)
, I can scroll "TUE 21", go to "WED 22", and go back to "TUE 21" and scroll the list with no issues. But as soon as I try to access a tab outside the offscreen page limit ("THU 23" or "FRI 24"), I see the bug.