Silicon Valley News 5--Top News Carousel Event handling

Source: Internet
Author: User

Rewrite the dispatchtouchevent, and GetParent (). Requestdisallowintercepttouchevent (True) when pressed.

Determine the sliding direction, the absolute value of sliding in the X-and y-axis who is big is sliding in what direction

One, Slide getParent () in the vertical direction. Requestdisallowintercepttouchevent (FALSE);

Two, horizontal direction sliding

2.1, when sliding to the No. 0 page of Viewpager, and is sliding from left to right getParent (). Requestdisallowintercepttouchevent (false);

2.2, when sliding to the last page of Viewpager, and is sliding from right to left getParent (). Requestdisallowintercepttouchevent (false); 2.3, other getParent (). Requestdisallowintercepttouchevent (True);

 Public classHorizontalscrollviewpagerextendsViewpager { PublicHorizontalscrollviewpager (Context context) {Super(context); }     PublicHorizontalscrollviewpager (Context context, AttributeSet attrs) {Super(context, attrs); }    /*** Starting coordinates*/    Private floatStartX; Private floatStarty; @Override Public Booleandispatchtouchevent (motionevent ev) {Switch(Ev.getaction ()) { CaseMotionevent.action_down://requests that the parent layer view does not intercept the current control's eventsGetParent (). Requestdisallowintercepttouchevent (true);//passes events to the current control (Horizontalscrollviewpager)//1. Record the starting coordinatesStartX =Ev.getx (); Starty=ev.gety ();  Break;  CaseMotionevent.action_move://2. Come to the new coordinates                floatEndX =Ev.getx (); floatEndY =ev.gety (); //3. Calculate the offset                floatDistancex = EndX-StartX; floatDistancey = EndY-Starty; //4. Determine the direction of the slide                if(Math.Abs (Distancex) >Math.Abs (Distancey)) {                    //Slide Horizontally//2.1, when sliding to the No. 0 page of Viewpager, and is sliding from left to right//getParent (). Requestdisallowintercepttouchevent (false);                    if(Getcurrentitem () ==0&&distancex >0) {getParent (). Requestdisallowintercepttouchevent (false); }//2.2, when sliding to the last page of Viewpager, and is sliding from right to left//getParent (). Requestdisallowintercepttouchevent (false);                    Else if((getcurrentitem () = = (Getadapter (). GetCount ()-1)) && Distancex <0) {getParent (). Requestdisallowintercepttouchevent (false); }//2.3, other, middle section//getParent (). Requestdisallowintercepttouchevent (true);                    Else{getParent (). Requestdisallowintercepttouchevent (true); }                }Else{                    //slide in the vertical directionGetParent (). Requestdisallowintercepttouchevent (false); }                 Break;  Casemotionevent.action_up: Break; default:                 Break; }        return Super. dispatchtouchevent (EV); }}

Silicon Valley News 5--Top News Carousel Event handling

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.