Implementation of circular advertising space using viewpager in Android
How to realize circular playback
Now the loop playback on the Internet is realized by returning a large value in the getcount () method of the adapter, and the instantiateitem (ViewGroup container, int position) makes the viewpager play continuously by taking the remainder (position / data. Size()).
Here we implement it by modifying the data source and setting the currentitem.
Modify data source:
Setting currentitem needs to be done in onpagechangelistener object:
The adapter is the same as the general viewpager adapter.
How to realize automatic circulation:
There are many methods to realize automatic loop, such as timer, handler, alarm, etc. Here we use handle
The custom handler class is as follows:
After obtaining the data and setting the adapter for the viewpager, you can start the automatic loop:
Here we use weak references, mainly to prevent memory overflow.
be careful:
When setting currentitem, be sure to set it in onpagechangelistener. A white screen will appear when setting it in finishupdate (view) of the adapter. If any God knows, please give me advice.