Android uses viewpager to automatically scroll round robin effects
For Android, use viewpager to realize the effect that pictures can slide left and right, and interested friends can directly click to view the content details.
This paper mainly introduces how to realize the effect and use of automatic playback and circular scrolling of viewpager. Incidentally, solve the problem that the viewpager inside (viewpager) affects the touch sliding and the setting of viewpager sliding speed.
First, let's show you the renderings. If you like, you can download the source code:
1. Realize
Instead of periodically executing a task through the scheduledexecutorservice or timer, simply send a message through the handler to complete a scroll, and send another delay scroll message after completing a scroll. This loop is implemented. The core code of automatic scrolling part is as follows:
Java
As for the problem that the child viewpager cannot be touched due to the nesting of viewpager, it is added in the ontouchevent of the child viewpager
Java
The parent control is prohibited from doing intercept solution to touch event.
The setting of the sliding speed of the viewpager is completed by resetting the scroller of the viewpager through reflection and changing the interval of the start scroll of the scroller. Just call setscrolldurationfactor (double).
2. Use
(1) Import public library
Introducing Android auto scroll ViewPager@Github As the library of your project (how to pull code and add public libraries).
(2) Call
Just two simple steps:
a. Layout definition
Java
Replace the general viewpager definition
b. Start viewpager auto scroll
Startautoscroll() starts autoscroll() stops autoscroll()
3. Set
Setinterval (long) sets the interval time of automatic scrolling in milliseconds. Setdirection (int) sets the direction of automatic scrolling. By default, setcycle (Boolean) to the right automatically circulates. By default, it is true. Setscrolldurationfactor (double) sets the magnification of the interval time of viewpager sliding animation to slow down the animation or change the animation speed. Setstopscrollwhentouch (Boolean) whether to stop automatic scrolling when the finger touches the viewpager. The default is true. Setslidebordermode (int) slides to the first or last item. It supports three modes: no operation, rotation and passing to the parent view. Setbordernanimation (Boolean) sets whether animation is required to scroll from the edge to the next during circular scrolling. The default is true
4. Other
(1) Indicator, round or square indicator, please use with viewpagerindicator (2) infinite loop. If you want to continue playing the first one in the last one instead of returning to the first one, please refer to autoscrollviewpagersingledemo.java. Note that this feature cannot be used with viewpagerindicator