Home » Android » android – Stop user scroll to set position on RecyclerView

android – Stop user scroll to set position on RecyclerView

Posted by: admin June 15, 2020 Leave a comment


So I’ve got a problem, i’m quite new to android programming, it’s been only 6-8 weeks since i’ve started programming android apps so i may be blind and not see the easy answer but here’s my problem:

I’m making a school project and i’m trying to develop a similar calendar to the one in Today’s Calendar split view (The one below here)

Image 1

So i’ve made a RecyclerView with a GridLayoutManager that looks like this:

Image 2

And up until here everything’s fine, but i’m trying to implement the button that sets the calendar on today’s date, the thing is that when i’m scrolling with a fling and then set the RecyclerView position with scrollToPositionWithOffset(position, 0); the view keeps the momentum gained from the first fling, sets the position and then keeps on scrolling until the momentum is gone

I’ve searched a lot to find a solution, when i was using gridView instead of RecyclerView i used smoothScrollToPositionFromTop() which stopped the scrolling and then the setSelection() to position it, but the gridView skipped frames and i had to change, here with RecyclerView there is no smoothScrollToPositionFromTop and smoothScrollToPosition doesn’t stop the scroll, neither does scrollToPosition or scrollToPositionWithOffset.

found this but doesn’t help Scroll RecyclerView to show selected item on top

and many others that didn’t help.

I considered implementing my own scrolling logic but when i looked into it my head exploded and i wouldn’t know where to begin.

Does anyone know how to solve this?

How to&Answers:

Did you know that RecyclerView has a stopScroll() method to stop a scroll in progress?