Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

I use attachToRecyclerView(RecyclerView) and it works as expected.

Question: at some point I want the same recyclerView to scroll normally, how to achieve that?

Code:

PagerSnapHelper pagerSnapHelper = new PagerSnapHelper();
pagerSnapHelper.attachToRecyclerView(recyclerView);
                did you read SnapHelper#attachToRecyclerView docs? whats unclear in the official documentation? "Attaches the SnapHelper to the provided RecyclerView, by calling setOnFlingListener(RecyclerView.OnFlingListener). You can call this method with null to detach it from the current RecyclerView."
– pskink
                Jul 26, 2017 at 8:05
                just tested with com.android.support:recyclerview-v7:25.1.0 - you can use attachToRecyclerView to attach / detach the same SnapHelper as many times as you want
– pskink
                Jul 26, 2017 at 9:07

Staring from recyclerview-v7:25.1.0, is enough to call

snapHelper.attachToRecyclerView(null); 

to remove the SnapHelper

I faced the same problem. Calling

clearOnScrollListeners();
setOnFlingListener(null);

on the RecyclerView instance did the trick for me. Calling only setOnFlingListener(null); wasn't enough in my case

well,it's written on the SnapHelper official API:Public methods=>attachToRecyclerView:[The RecyclerView instance to which you want to add this helper or null if you want to remove SnapHelper from the current RecyclerView.] https://developer.android.com/reference/android/support/v7/widget/SnapHelper.html#attachToRecyclerView%28android.support.v7.widget.RecyclerView%29

This is should be the accepted answer. And please note that if your app allow switching between several LayoutManagers on a RecyclerView, like Grid layout and SnapPage layout, don't forget to detach/remove the SnapHelper from your RecyclerView (after switched to Grid view). Otherwise you will see unexpected behaviour while scrolling or flinging on the Grid view list. – Wei WANG Oct 11, 2017 at 5:01

If you don't want to keep the reference of SnapHelper then there is another way around as per the official documentation you can use

recyclerView.setOnFlingListener(null);

From the Android documentaion

  • Attaches the {@link SnapHelper} to the provided RecyclerView, by calling
  • {@link RecyclerView#setOnFlingListener(RecyclerView.OnFlingListener)}.
  • You can call this method with {@code null} to detach it from the current RecyclerView.
  • Caution

    SnapHelper.attachToRecyclerView() can throw IllegalArgumentException

  • @throws IllegalArgumentException if there is already a {@link RecyclerView.OnFlingListener}
  • attached to the provided {@link RecyclerView}.
  • Thanks for contributing an answer to Stack Overflow!

    • Please be sure to answer the question. Provide details and share your research!

    But avoid

    • Asking for help, clarification, or responding to other answers.
    • Making statements based on opinion; back them up with references or personal experience.

    To learn more, see our tips on writing great answers.