Viewpager multiple page swipe toggle + cursor Swipe

Source: Internet
Author: User

Let's look at it first.

is what we need to achieve, the function has the following three:

1. Gesture swipe can switch the interface, click on the boot one to boot four can also switch to the corresponding screen.

2. When you switch the interface, the horizontal line below the guide will move to the corresponding place.

3. When there is no gesture, the interface will rotate automatically, there is the effect of advertising rotation, the gesture is, will stop rotation.

The second function is implemented here.

Thought: To achieve this effect is actually very simple. On the one hand, we need to initialize the state of the cursor, first get the width of a textview, and then set the width of the imageview of the cursor to this size, to ensure textview and ImageView alignment. Then you need to listen to the Viewpager, whenever the viewpager occurs, set a translation animation to ImageView, so that it translates to the corresponding textview.

Initialize cursor State:

Private voidInitcursor () {cursor=(ImageView) Findviewbyid (r.id.cursor); Cursorwidth=Bitmapfactory.decoderesource (Getresources (), r.drawable.cursor). GetWidth (); Displaymetrics DM=NewDisplaymetrics ();        Getwindowmanager (). Getdefaultdisplay (). Getmetrics (DM); intWindowWidth = Dm.widthpixels;//Get resolution width        intTabwidth = windowwidth/listview.size ();
Will beThe cursor is set to the same width as the textview.
cursor.getlayoutparams (). Width=tabwidth; Cursorwidth=Tabwidth;
}

To set the listener for Viewpager:

We need to monitor the slide of the Viewpager, and then shift the cursor to the bottom according to the slide accordingly. Viewpager's Onpagechangelistener has been introduced in the previous article.

What we need here is the onpageselected (int arg0), which sets the translation animation of the cursor after the page has finished jumping.

 ......  Public void onpageselected (int  arg0) {            new translateanimation (cursorwidth*  CurrentPage, Cursorwidth                    * arg0, 0, 0);            Animation.setduration (+);
//refers to the end of the animation is the screen stays in this animation the last frame Animation.setfillafter (true); Cursor.startanimation (animation); = arg0; } ......

Once this is done, our functionality is realized.

Viewpager multiple page swipe toggle + cursor Swipe

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.