Original article:
Scrollview nested scrollview
Http://www.eoeandroid.com/thread-240709-1-1.html
As we all know, two scrollviews in the same direction in Android cannot be nested. What if there is such a requirement? (This requirement is generally raised by people who do not understand Android)
Is it true that it cannot be nested? Of course you can. As long as you write another scrollview and set the foot in it, it supports nesting.
Currently, only two scrollviews can be nested, and more than two views need to be improved. This can meet many requirements. In addition, we only support the vertical scrollview, I am not eager to do this horizontally.
Effect:
First go to the coreCodeRight. I added comments in the code for your convenience.
Package Com. Sun. Shine. Study. innerscrollview. view; Import Android. content. context; Import Android. util. attributeset; Import Android. View. motionevent; Import Android. View. view; Import Android. widget. scrollview; Public Class Innerscrollview Extends Scrollview { /** */ Public Scrollview parentscrollview; Public Innerscrollview (context, attributeset attrs ){ Super (Context, attrs );} Private Int Lastscrolldelta = 0 ; Public Void Resume () {overscrollby ( 0,-lastscrolldelta, 0, getscrolly (), 0, getscrollrange (), 0, 0, True ); Lastscrolldelta = 0 ;} Int MTop = 10 ; /** * Roll targetview to the top */ Public Void Scrollto (view targetview ){ Int Oldscrolly = Getscrolly (); Int Top = targetview. gettop ()- MTop; Int Delaty = Top- Oldscrolly; lastscrolldelta =Delaty; overscrollby ( 0, delaty, 0, getscrolly (), 0, getscrollrange (), 0, 0, True );} Private Int Getscrollrange (){ Int Scrollrange = 0 ; If (Getchildcount ()> 0 ) {View child = Getchildat (0 ); Scrollrange = Math. Max (0, child. getheight ()-(Getheight ()));} Return Scrollrange ;} Int Currenty; @ override Public Boolean Onintercepttouchevent (motionevent eV ){ If (Parentscrollview = Null ){ Return Super . Onintercepttouchevent (EV );} Else { If (EV. getaction () = Motionevent. action_down ){ // Blocks the rolling event of the parent scrollview Currenty = ( Int ) EV. Gety (); setparentscrollable ( False ); Return Super . Onintercepttouchevent (EV );} Else If (EV. getaction () = Motionevent. action_up ){ // Restore the rolling event to the parent scrollview Setparentscrollable ( True );} Else If (EV. getaction () = Motionevent. action_move ){}} Return Super . Onintercepttouchevent (EV) ;}@ override Public Boolean Ontouchevent (motionevent eV) {view child = Getchildat (0 ); If (Parentscrollview! = Null ){ If (EV. getaction () = Motionevent. action_move ){ Int Height = Child. getmeasuredheight (); Height = Height- Getmeasuredheight (); // System. Out. println ("Height =" + height ); Int Scrolly = Getscrolly (); // System. Out. println ("scrolly" + scrolly ); Int Y = ( Int ) EV. Gety (); // Slide your finger down If (Currenty < Y ){ If (Scrolly <= 0 ){ // If you scroll down to the header, the scroll is handed over to the parent scrollview. Setparentscrollable ( True ); Return False ;} Else {Setparentscrollable ( False );}} Else If (Currenty> Y ){ If (Scrolly> = Height ){ // If you slide upwards to the header, the scroll is handed over to the parent scrollview. Setparentscrollable ( True ); Return False ;} Else {Setparentscrollable ( False ) ;}} Currenty = Y ;}} Return Super . Ontouchevent (EV );} /** * Whether to give the rolling event to the parent scrollview ** @ Param Flag */ Private Void Setparentscrollable ( Boolean Flag) {parentscrollview. requestdisallowintercepttouchevent ( ! Flag );}}