bgogetap / StickyHeaders

Easily add Sticky Headers to your RecyclerView
Apache License 2.0
521 stars 88 forks source link

Sticky header gets stuck when returning to the view within a ViewPager #99

Closed ryandt closed 3 years ago

ryandt commented 4 years ago

When presenting a list of items with sticky headers within a Fragment that's hosted by a ViewPager2, a sticky header bug occurs when moving back to the page from another page in the ViewPager2.

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:

  1. Open the app to view the list of items on the "TUE 21" tab.
  2. Scroll away from list position 0 so that some other header becomes sticky. "Header at 52", for example.
  3. Swipe to or click on the "THU 23" tab.
  4. Swipe back to or click on the "TUE 21" tab. "TUE 21"'s state should still be preserved so that "Header at 52" is still sticky.
  5. Scroll up to the top of the list.

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 call ViewPager2.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.

bgogetap commented 4 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.

vunder commented 3 years ago

Problem still present. Any updates on this?

bgogetap commented 3 years ago

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.