Everyone, as we all know, two scrollview in the same direction in Android can not be nested, what if there is such a demand? (This requirement is usually made by people who don't know Android)
Is it really impossible to nest? Of course, as long as you write a scrollview, in the inside to do a bit of foot, it supports nesting.
Currently do this only support two scrollview nesting, more than two have to be improved, can set two have been able to meet a lot of needs, hehe, and now only do the longitudinal scrollview support, horizontal has not come to do oh.
screenshot of Effect:
Let's go to the core code. Inside the code I added a note to make it easier for everyone to read
Copy Code code as follows:
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 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 the 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) {
To intercept a parent ScrollView's scrolling event
CurrentY = (int) ev.gety ();
Setparentscrollable (FALSE);
return super.onintercepttouchevent (EV);
else if (ev.getaction () = = motionevent.action_up) {
Restores the scrolling 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 fingers down.
if (CurrentY < y) {
if (scrolly <= 0) {
If you slide the head down, roll over to the parent ScrollView
Setparentscrollable (TRUE);
return false;
} else {
Setparentscrollable (FALSE);
}
else if (CurrentY > Y) {
if (scrolly >= height) {
If you slide the head up, roll 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);
}
}