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?