Nested ScrollView in ScrollView of android)

Source: Internet
Author: User

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: <ignore_js_op>

Upload at, January 20

Download Attachment(338.36 KB)


Let's start with the core code. 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 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 ){
// Intercept 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 slide down to the header, give the scroll 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, give the scroll 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 );
}

}

 

Reproduced in: http://www.eoeandroid.com/thread-240709-1-1.html

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.