Android viewpager realizes intelligent infinite loop scrolling and wrapping effect

The standard way of viewpager provided by Android system is that you can slide freely from left to right, but the first page is the extreme position to the left, and the last page is the position to the right. When you slide to the left or right, you can't slide / scroll anymore. This is the default implementation way of viewpager in Android system.

However, in some cases, developers may want viewpager to intelligently scroll around in an infinite loop. For example, there are five pages numbered 1,2,3,4,5.

(1) When the user's finger scrolls from right to left to the rightmost / rearmost page 5, if the user continues to drag the viewpager to the left, the viewpager will wrap around and cycle to the first page - > 1, followed by 2,5;

(2) Conversely, if the user's finger slides from left to right to the first left page: 1, if he continues to drag the viewpager and continues to slide from left to right, it will wrap around to 5, followed by 4,1

We call this kind of viewpager "infinite loop scroll back" viewpager.

There are many ways to implement this type of viewpager on the Internet. Now we give a code implementation with a wide process.

Write a test mainactivity. Java:

Mainactivity.java requires a layout file activity_ main.xml:

The core key code is loopviewpager.java class and looppageradapterwrapper.java, which loopviewpager.java depends on!

LoopViewPager.java:

LoopPagerAdapterWrapper.java:

If readers are interested in using, they can directly put the above two core key codes into their own project code package and use them directly as their own classes. When writing the layout, do not write viewpager, but use loopviewpager directly like the layout file I wrote above.

The above is the whole content of this article. I hope it will be helpful to your study, and I hope you can support programming tips.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>