directory

What is a SnapHelper

SnapHelper is Google in Android 24.2.0 support package added to expand RecyclerView, combined with RecyclerView use, can be very convenient to make some cool effects.

How to use SnapHelper

SnapHelper is an abstract class. Google has two built-in default implementation classes, LinearSnapHelper and PagerSnapHelper.

  • LinearSnapHelper: Center the current Item. The common scenario is a horizontal RecyclerView, which is similar to the ViewPager effect, but can quickly slide multiple items.
LinearLayoutManager manager = new LinearLayoutManager(getContext());
manager.setOrientation(LinearLayoutManager.VERTICAL);
mRecyclerView.setLayoutManager(manager);
LinearSnapHelper snapHelper = new LinearSnapHelper();
snapHelper.attachToRecyclerView(mRecyclerView);

Copy the code
  • PagerSnapHelper: Make RecyclerView look like ViewPager, only slide one page at a time.
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
linearLayoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
mRecycleview.setLayoutManager(linearLayoutManager);
PagerSnapHelper snapHelper = new PagerSnapHelper();
snapHelper.attachToRecyclerView(mRecycleview);

Copy the code

Here I just use PagerSnapHelper for an example:

Project source: github.com/myml666/Sna…