I very recently started using the SnapHelper
provided inside the Support Library v24
in order to have a nice horizontal RecyclerView
much like in the Google Play Store.
My question is simple.
By using a RecyclerView.OnScrollListener
I can be notified when the scroll state is settling (much like with ViewPager.OnPageChangedListener
. This is important because when the scroll is settling, I need to animate the view that will be centered as the default behavior of the LinearSnapHelper
. That animation shows that the focus is brought to the soon-to-be centered item.
For now, I managed to get the view that is centered when the scroll state is idle via the following line:
final int centered = (llm.findLastCompletelyVisibleItemPosition() - llm.findFirstCompletelyVisibleItemPosition()) / 2 + llm.findFirstVisibleItemPosition(); // where llm is my LinearLayoutManager
Problem is, starting the animation when the scroll is completed is weird: I really need to smoothly play the animation as the scroll is settling so that when the scroll is idle, the animation completes as well.
So as you can see, I have hard time finding out which item of my RecyclerView
will be the one centered.
Here are the solutions I tried so far.
I tried using the same calculation when scroll state is set to
RecyclerView.SCROLL_STATE_SETTLING
. Unfortunately, calling thefindVisiblePosition
methods from the adapter when the scroll isn't idle is a bad idea : depending on the scroll speed, the visible items, when these so-called methods are invoked, might not be exactly the same when the scroll is complete. Indeed it will tell me which items are visible at the time of the call. That makes total sense but I needed to try to find that out...I tried extending the
LinearSnapHelper
so that whenonFling(...)
is called I can either usefindSnapView(...)
to get the view that will be snapped or usefindTargetSnapPosition(...)
hopefully telling me which view is to be centered when the scroll completes ;I also tried, when scroll state is
RecyclerView.SCROLL_STATE_SETTLING
, to usefindSnapView(...)
Of all these solutions, none passed multiple tests (trying with different swipe strength & speed). Some got it right on a few occasions but got it wrong in different circumstances therefore rendering the solution unusable.
Having looked inside the LinearSnapHelper
I think I started to understand that, somehow, after a swipe, this class computes the total scroll and snaps views on multiple occasions as long as necessary before the scroll completes (amount of distance to scroll) therefore knowing which view will be the final snapped view ... unless I got it all wrong because snapped is not centered?
Anyway, my main question is: How can I know which item of my RecyclerView
will be centered after a swipe, regardless of speed and strength of said swipe?