Achieve an infinite loop ViewPager carousel

Disclaimer: This article is a blogger original article, follow the CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source link and this statement.
This link: https://blog.csdn.net/Utzw0p0985/article/details/97987985

This article first planet [I] Hi Android reading about five minutes

We occasionally encounter when advertising page of this demand, is the need for direct unlimited slide carousel, then this needs to do it? I Before comparing opportunistic, by using a timer to switch Handler, when switching to the last one on the switch back to the first, but this experience is not very good, today we have to realize what an infinite loop ViewPager carousel advertising it first look ViewPager wheel under normal sowing how to achieve, look at renderings:

       

640?wx_fmt=gif

     

 Review the implementation process ViewPager, in fact, as long as the data source to fill the adapter, can be realized, then we first to write a adapter it:

     640?wx_fmt=jpeg      

Smart PagerAdapter relatively simple to achieve, are common ways to achieve the following methods after inheritance PagerAdapter, then that is specific code implementation:

       640?wx_fmt=jpeg      

 

Specific implementation, where the five preset picture on the List, and by addOnPageChangeListener monitor the switching states to update subscript show that, overall ViewPager is relatively simple and straightforward, so now we are going on this basis up unlimited the infinite carousel.

 

In fact most of the online method, you want to achieve unlimited carousel, as long as the start of the Adapter in getCount like, look at me revise the code:

       640?wx_fmt=jpeg      

This adapter I modified the total of three places, the first is getCount, I do not return List Size, and I set to the maximum Int directly, that is Integer.MAX_VALUE = 2147483647, so that he could slide unlimited, but it's just our index, we can not create it so much View, the View so we want to reuse, using modulo get real index, it is the same as instantiateItem and destroyItem, the current position% size on get real index, and we have to get real index, also onPageSelected added to the modulo, do not write out here, running facie effect:

 

       

640?wx_fmt=gif

     

 

So that we realize the infinite slide, but when left slip past you will find not draw, because ViewPager initialization value is 0, then you count from zero certainly does not work, then we need to how to do it?

 

The idea is to make the result of nothing more than change the default values, then change where? Change to the middle to go, since we used the Integer.MAX_VALUE, then used his divided by 2 is not half, but the values ​​obtained are sometimes not marked at 0, that is not true for our first chart, so we need to make some changes.

       640?wx_fmt=png    

Here is the wording trap, assuming that half of the value in the third, then I subtract half the value of the third, not to get the first. In this way we run no problem.

 

Well, to sum up the idea of ​​it:

To achieve unlimited carousel, only need to modify the return value getCount, and the need to use the position where modulo, because their index has changed, in order to optimize the left slide, the default initial index in the data source intermediate.

 

Well, you got it?

 

Source Downloads:

https://pan.baidu.com/s/1urTVy7p8F7vKv-XEGC_9yw 

Extraction code: q8s0 

Interested parties can join my knowledge of the planet, or click below to read the original text:

Welcome to the knowledge of the planet [Hi Android]

Guess you like

Origin blog.csdn.net/Utzw0p0985/article/details/97987985