Google I / O 2019 Update:
ViewPager2 is here!
Google just announced during the “What's New in Android” (also known as “Android Summary”) performance that they are working on a new RecyclerView ViewPager!
From the slides:
Like ViewPager, but better
- Simple migration from ViewPager
- Based on RecyclerView
- Right to left support
- Allows vertical paging
- Improved data set change notifications
You can check the latest version here and release notes here . There is also an official sample .
Personal opinion: I think this is a really necessary addition. Recently, I had a lot of problems with PagerSnapHelper swaying left and right indefinitely - see the ticket I opened.
New answer (2016):
Now you can just use SnapHelper .
If you want the center snap to look like a ViewPager , use the PagerSnapHelper :
SnapHelper snapHelper = new PagerSnapHelper(); snapHelper.attachToRecyclerView(recyclerView);
There is also LinearSnapHelper . I tried this, and if you throw with energy, it scrolls 2 objects with 1 throw. Personally, I did not like this, but I decided on my own - the attempt will take only a few seconds.
Original answer (2016):
After many hours of trying to find the 3 different solutions found here in SO, I finally found a solution that very closely mimics the behavior found in ViewPager .
The solution is based on the @eDizzle solution , which I think I have improved enough to say that it works almost like a ViewPager .
Important: the width of the RecyclerView elements is exactly the same as the screen I have not tried with other sizes. I also use it with a horizontal LinearLayoutManager . I think you will need to adapt the code if you want to scroll vertically.
Here you have the code:
public class SnappyRecyclerView extends RecyclerView {
Enjoy it!
Albert Vila Calvo Jun 14 '16 at 16:01 2016-06-14 16:01
source share