Android:scrollview Monitor swipe to top and bottom

Source: Internet
Author: User

In the project need to listen ScrollView sliding to the top and bottom of the time to achieve their own scrollview, then how to listen to it? Today, a look at the source of ScrollView, found a way. Let's take a look at the Overscrollby () method in the source code:

1 protected BooleanOverscrollby (intDeltaX,intDeltaY,2             intSCROLLX,intscrolly,3             intScrollrangex,intScrollrangey,4             intMAXOVERSCROLLX,intmaxoverscrolly,5             Booleanistouchevent) {6         Final intOverscrollmode =Moverscrollmode;7         Final BooleanCanscrollhorizontal =8Computehorizontalscrollrange () >computehorizontalscrollextent ();9         Final BooleanCanscrollvertical =TenComputeverticalscrollrange () >computeverticalscrollextent (); One         Final BooleanOverscrollhorizontal = Overscrollmode = = Over_scroll_always | | A(Overscrollmode = = Over_scroll_if_content_scrolls &&canscrollhorizontal); -         Final Booleanoverscrollvertical = Overscrollmode = = Over_scroll_always | | -(Overscrollmode = = Over_scroll_if_content_scrolls &&canscrollvertical); the         intNEWSCROLLX = Scrollx +DeltaX; -         if(!overscrollhorizontal) { -MAXOVERSCROLLX = 0; -         } +         intnewscrolly = scrolly +DeltaY; -         if(!overscrollvertical) { +maxoverscrolly = 0; A         } at         //Clamp values if at the limits and record -         Final intleft =-Maxoverscrollx; -         Final intright = Maxoverscrollx +Scrollrangex; -         Final inttop =-maxoverscrolly; -         Final intBottom = maxoverscrolly +Scrollrangey; -         BooleanClampedx =false; in         if(Newscrollx >Right ) { -NEWSCROLLX =Right ; toClampedx =true; +}Else if(Newscrollx <Left ) { -NEWSCROLLX =Left ; theClampedx =true; *         } $         BooleanClampedy =false;Panax Notoginseng         if(Newscrolly >bottom) { -newscrolly =Bottom; theClampedy =true; +}Else if(Newscrolly <top) { Anewscrolly =top; theClampedy =true; +         } - onoverscrolled (NEWSCROLLX, newscrolly, Clampedx, clampedy); $         returnClampedx | |Clampedy; $}

This method has 9 parameters, and the parameters are too many to blur. Here we don't care what they mean, just look at 36-43 lines of code, obviously this is a vertical boundary check, and that's what we want. So we can override this method to implement listening in this method, the code is as follows:

1 @Override2     protected BooleanOverscrollby (intDeltaX,intDeltaY,intSCROLLX,intScrolly,intScrollrangex,intScrollrangey,intMAXOVERSCROLLX,intMaxoverscrolly,Booleanistouchevent) {3 4         intnewscrolly = scrolly +DeltaY;5         Final intBottom = maxoverscrolly +Scrollrangey;6         Final inttop =-maxoverscrolly;7         if(Newscrolly >bottom) {8System.out.println ("Swipe to end");9         }Ten         Else if(Newscrolly <top) { OneSystem.out.println ("Swipe to top"); A         } -         return Super. Overscrollby (DeltaX, DeltaY, Scrollx, scrolly, Scrollrangex, Scrollrangey, Maxoverscrollx, MaxOverScrollY, istouchevent); -}

Isn't it simple?

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.