Use RecyclerView + SnapHelper achieve a similar effect ViewPager

The paper notes just to be a point of knowledge, but also reference other bloggers to write something, I did not explain the source of the record, just simply understand the effect SnapHelper and usage, in order to understand the source code, please refer to the original blogger blog , the original link: https: //blog.csdn.net/ChrisSen/article/details/80676459 personally feel that the bloggers write very detailed and easy to understand.

If we normally use RecyclerView, then the effect we achieve it should look like: When we slide, RecyclerView in Item stop position when the speed (Fling) when depends on your slide, may occur our final stop position may be located between two item, of course, there may be just in the middle of an item.

If this time we made the final product manager position must be stopped at a certain Item, located between the two situations can not occur; or product manager, said slide when we slip one by one in the past, you can not decline a good number ...

Let's take a look at the results.

 

Effect: After sliding stop Item center will be attached to the center RecyclerView, saying that the straightforward point is to stop the slide show will be a complete Item.

Effect: a sliding a sliding, sliding effect similar ViewPager.

SnapHelper

To achieve the effect of the above two classes used is SnapHelper.

In fact, the role and effectiveness of SnapHelper rewrite onFling () is the same, but not listening mode used, instead of using a special class to handle.

Snapping What is it? Google has translated the meaning of cards, broken, suddenly, fold meaning. I feel like not take the edge ah. My understanding is: Snap on behalf of a state, this state is in a transition state between movement and stillness. For RecyclerView it is the state when stopping the slide after treatment. For example, the first effect we achieve, when RecyclerView at rest, the way SnapHelper treatment is to stop after Item center attached to the center of RecyclerView. The second embodiment is the treatment effect SnapHelper the next slide Item center attached RecyclerView.

SnapHelper is an abstract class that inherits from RecyclerView.OnFlingListener, OnFlingListener only an abstract method onFling (int velocityX, int velocityY). onFling () method is mainly used to process results fling. SnapHelper has two direct subclasses: LinerSnapHelper, PagerSnapHelper. LinerSnapHelper mainly used to achieve the first effect, PagerSnapHelper ViewPager used to achieve similar results. They both use very simple.

LinearSnapHelper linearSnapHelper = new LinearSnapHelper();
linearSnapHelper.attachToRecyclerView(mRecyclerView);

  

PagerSnapHelper pagerSnapHelper = new PagerSnapHelper();
pagerSnapHelper.attachToRecyclerView(mRecyclerView);

Just two lines of code you can achieve the desired effect.

 

Guess you like

Origin www.cnblogs.com/huiing/p/11641064.html