How to remove SnapHelper from a RecyclerView

Viewed 4194

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);
3 Answers

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}.
Related