Android uses Viewpager automatic rolling cycle carousel effect _android

Source: Internet
Author: User
Tags time interval

For Android use Viewpager to realize the image can be around the circular sliding effect, interested friends can directly click to see the content details.

This paper mainly introduces how to realize Viewpager automatic playback, the effect and use of cyclic scrolling. By the way, the problem of viewpager nesting (Viewpager inside viewpager) affecting touch sliding and viewpager sliding speed setting is solved.

First to show you the effect of the picture, like friends can download the source code:

1. Realize

Do not perform a task on a regular basis through scheduledexecutorservice or timer, but simply send a message through handler to complete a scroll, and then send another delay scroll message after the completion of a scrolling, so the loop implementation. The core code for the automatic scrolling section is as follows:

Java

 public void Startautoscroll () {
 isautoscroll = true;
 Sendscrollmessage (interval);
 }
 private void Sendscrollmessage (long delaytimeinmills) {
 /** remove messages before, keeps one message are running at MO St **/
 handler.removemessages (scroll_what);
 Handler.sendemptymessagedelayed (Scroll_what, delaytimeinmills);
 }
 Private class MyHandler extends Handler {
 @Override public
 void Handlemessage (msg) {
  Super.handlemessage (msg);
  Switch (msg.what) {case
  scroll_what:
   scrollonce ();
   Sendscrollmessage (interval);
   break;
  }}}
 

As for viewpager nesting causes the child Viewpager to touch the problem is by adding in the ontouchevent of the child Viewpager

Java

GetParent (). Requestdisallowintercepttouchevent (True);

Prevents the parent control from intercept the touch event.

The setting of the sliding speed of viewpager is to reset the Viewpager scroller by reflection, and to change the scroller startscroll interval time. Call Setscrolldurationfactor (double).

2. Use

(1) Introduction of public libraries

Introduce the Android Auto Scroll Viewpager@github as your project library (how to pull the substitution code and add a public library).

(2) Call

It takes only two simple steps:

A. Layout definition

Java

<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 automatic scrolling

Startautoscroll () Start automatic scrolling
Stopautoscroll () Stop automatic scrolling

3, set up

SetInterval (long) sets the time interval for automatic scrolling in milliseconds
setdirection (int) sets the direction of automatic scrolling, by default to the right
Whether Setcycle (Boolean) automatically loops the carousel, the default is True
Setscrolldurationfactor (double) sets the magnification of the Viewpager sliding animation interval to achieve slow animation or change animation speed
Setstopscrollwhentouch (Boolean) whether to stop automatic scrolling when the finger touches the Viewpager, default to True
Setslidebordermode (int) slides to the first or last item for processing, supporting no operations, rotations, and passing to parent view three modes
Setborderanimation (Boolean) sets whether the animation is required to scroll from the edge to the next when the loop scrolls, and the default is True

4. Other

(1) indicator, round or square indicator, please use with Viewpagerindicator
(2) infinite loop, if you want to continue to play the first one in the last one instead of back to the first one, please refer to Autoscrollviewpagersingledemo.java, note that this feature can not be used with Viewpagerindicator

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.