It looks like that the most popular answer on the internet for
implementing a looping/infinite ViewPager in Android is as shown below.
I was like when I first saw this approach. Anyway, after some
research I finally found a correct way to implement this thing.
Correct Approach
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:
[C][A][B][C][A]
We start with position 1.
When hit position 0 -> jump to position 3.
When hit position 4 -> jump to position 1.
Let’s Get to Codes
NOTE: 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.