Main Introduction
How to realize Viewpager auto-play, the effect of cyclic scrolling and its use. by the way to resolve
Viewpager nesting (Viewpager inside viewpager) affects touch sliding and viewpager sliding speed setup Issues.
Project has open source Android Auto Scroll [email protected], Welcome to star and Fork.
Sample APK can be downloaded from these addresses: Google Play, 360 mobile phone assistant, Baidu Mobile assistant, Xiaomi App Store, Pea pod
The sample code address is shown in Autoscrollviewpagerdemo, as follows:
1. Realize
Instead of performing a task implementation on a regular basis through scheduledexecutorservice or a timer, it is simple to send a message through handler to complete a scroll, and to send another delay scrolling message after a single scroll is completed, so the loop is implemented. The automatic scrolling part of the core code is as follows:
Java
12345678910111213141516171819202122232425 |
Public void startautoscroll() { isautoscroll = true; sendscrollmessage(interval); } private void sendscrollmessage(long delaytimeinmills) { /** Remove messages before, keeps one message is running at most **/ handler. Removemessages(scroll_what); handler. Sendemptymessagedelayed(scroll_what, delaytimeinmills); } Private class myhandler extends Handler { @Override Public void handlemessage(Message msg) { Super. Handlemessage(msg); Switch (msg. What) { Case scroll_what: scrollonce(); sendscrollmessage(interval); Break ; } } } |
As for viewpager nesting causes child viewpager to be unable to touch the problem is by adding in the ontouchevent of the child Viewpager
Java
1 |
GetParent(). Requestdisallowintercepttouchevent(true); |
Prevents the parent control from intercept the touch event.
The setting of the Viewpager sliding speed is reset by the reflection of the Viewpager scroller, changing the scroller of the Startscroll interval time to complete. You can call Setscrolldurationfactor (double).
2. Use
(1) Introduction of public libraries
Introduce Android Auto Scroll [email protected] as your project library (how to pull a replacement code and add a public library).
(2) Call
Only two simple steps are required:
A. Layout definition
Java
1234 |
<cn. Trinea. Android. View. Autoscrollviewpager. Autoscrollviewpager Android:id="@+id/view_pager" android:layout_width="Match_parent" android:layout_height="wrap_content" /> |
Instead of the general Viewpager definition
B. Start Viewpager auto-scroll
Startautoscroll () Start automatic scrolling
Stopautoscroll () Stop auto-scrolling
3. Setting
SetInterval (long) sets the interval between automatic scrolling in milliseconds
setdirection (int) sets the direction of automatic scrolling, by default to the right
Setcycle (Boolean) whether to automatically cycle the carousel, by default True
Setscrolldurationfactor (double) sets the magnification of the Viewpager sliding animation interval to the effect of slowing the animation or altering the animation speed
Setstopscrollwhentouch (Boolean) whether to stop auto-scrolling when the finger touches Viewpager, by default True
Setslidebordermode (int) slides to the first or last item processing mode, supports no action, carousel, and passes to parent view three modes
Setborderanimation (Boolean) sets whether animation is required to scroll from edge to next when looping scrolling, by default true
4. Other
(1) indicator, round or square indicator please use with Viewpagerindicator
(2) infinite loop, if you want to continue playing the first sheet 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
Viewpager of Android Auto-scrolling rotation cycle