Modify the animation effect of viewpager Switching

Source: Internet
Author: User

For example, if we click the right button and want to move the view on the left to have a translation effect, the system will flash when switching with the default viewpager. Why?

Because viewpager actually has a scrollview, you can use the reflection method to set its Scroll time. The Code is as follows.

Main Code:

// Step 1:

Layoutinflater minflater = (layoutinflater) context. getsystemservice (context. layout_inflater_service); view = minflater. from (context ). inflate (R. layout. vw_personalcyclic gtarget_test, personalfilemain); mycyctargetviewpager MVP = (mycyctargetviewpager) view. findviewbyid (R. id. cyctarget_viewpager); try {// viepager translation animation event field mfield = viewpager. class. getdeclaredfield ("mscroller"); mfield. setaccessible (true); myscroller mscroller = new myscroller (MVP. getcontext (), new accelerateinterpolator (); mfield. set (MVP, mscroller);} catch (nosuchfieldexception e) {e. printstacktrace ();} catch (illegalargumentexception e) {e. printstacktrace ();} catch (illegalaccessexception e) {e. printstacktrace ();}
// Step 2:

/***** @ Desc: viewpager animation time * @ Author: pangzf * @ Date: august 13, 2014 6:00:05 */public class myscroller extends scroller {// set the translation time you need private int animtime = 500; Public myscroller (context) {super (context );} public myscroller (context, interpolator) {super (context, interpolator) ;}@ override public void startscroll (INT startx, int starty, int dx, int dy, int duration) {super. startscroll (startx, starty, dx, Dy, animtime) ;}@ override public void startscroll (INT startx, int starty, int dx, int Dy) {super. startscroll (startx, starty, dx, Dy, animtime);} public void setmduration (INT animtime) {This. animtime = animtime ;}
You cannot use viewpager to slide left or right in our project. You can click link to disable the sliding left or right.

Click the right button:



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.