9

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?

Wasim K. Memon
  • 5,979
  • 4
  • 40
  • 55
alessandro gaboardi
  • 889
  • 3
  • 11
  • 26

1 Answers1

48

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

kris larson
  • 30,387
  • 5
  • 62
  • 74
  • 1
    Oh my god i'm facepalming so hard right now :_) thank you very much. As soon as i can test it i'll accept your answer ;) – alessandro gaboardi Feb 27 '15 at 07:10
  • 2
    Hey, I've been bashing computers into submission for 30 years and still have a daily facepalm. Cheers – kris larson Feb 27 '15 at 13:23
  • Do you happen to know why the smoothscroll method of the recyclerView lags a lot when used on pre lollipop especially when using textViews? – alessandro gaboardi Mar 03 '15 at 00:08
  • When you say "lags", are you getting stutters in the scroll motion? I would make sure there isn't any logic executing on the UI thread that should be pushed off onto a background thread before suspecting the platform. I do get a lot of jerky behavior when running with an emulator or even having the device hooked up to the debugger, but running normally on a device I don't see that. – kris larson Mar 03 '15 at 01:56
  • I tried it on a blank activity with static content on the cell just a textview with "aaa" and on lollipop works just fine but on pre lollipop stutters a lot, it's almost like I used scrollTo instead of smoothScrollTo. When i used all Imageviews with ic_launcher instead of the textviews the stutter diminished a lot – alessandro gaboardi Mar 03 '15 at 06:14
  • Thank you very much, i've put my code here https://github.com/sedestrian/Scroll-Test is just a basic recyclerview with static content cells. I hope i've put everything needed :) – alessandro gaboardi Mar 04 '15 at 08:01
  • I will try it out. I was just thinking, I saw a demo animation for GMail in the Material Design docs, then when my GMail got updated I looked for the animation and it wasn't there. The app would have been using the support library since my tablet was still on KitKat. So I suspect that Google took the lazy way out in the support lib and didn't make it as fully featured as the Lollipop platform code, especially concerning animations. What I mean is the support library probably says, okay are we on Lollipop? then just pass off to the platform, otherwise do a not-as-good imitation. – kris larson Mar 04 '15 at 08:43
  • That might be, but then why make the method smoothScroll available for pre Lollipop? (I forgot to tell you that to try my code i did it a little ghetto, so you'll have to tap Settings to start the scroll XD) – alessandro gaboardi Mar 04 '15 at 08:47
  • @kris larson :- Is there is any method to start scroll RecyclerView again after stopping? – void pointer Feb 28 '16 at 14:14
  • @voidpointer I think the idea is that after stopping the scroll, you would use a method like `scrollToPositionWithOffset()` to move the list where you wanted, as in the OP's case. – kris larson Feb 29 '16 at 16:21
  • Thanks bro ... You save may One day !! – Sushant Gosavi Jul 28 '17 at 06:25