It looks like that the most popular answer on the internet for
implementing a looping/infinite ViewPager in Android is as shown below.
My face was like when I first saw this approach. Anyway, after some
research I finally found a correct way to implement this thing.
How to implement it correctly
The idea is quite simple. Let’s assume we have three pages A, B, C. The way
we want to put them in the ViewPager is like this:
We start with position 1.
When hit position 0 -> jump to position 3.
When hit position 4 -> jump to position 1.
Okay now let’s get to the code.
There’s a small trick here. Due to the way ViewPager is implemented, there’s no
way you can completely swipe to the next page. So when touch up, the ViewPager
will determine the next page according to your scroll position and animate to it.
The bad part about this is that, onPageSelected is hit when the animation
starts. So if you do the jump here you will cancel scroll animation and it will
look weird. The better solution is to wait for the animation to complete and
then do the jump.