Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to detect overscroll in Android ListView?

I'd like to show/hide a view when the user over scroll the listview.

How can I detect the over-scroll? Is there any listener? I've tried OnScrollListener but it only notifies about onScrollStateChanged and onScroll

like image 631
Alexey Avatar asked Oct 12 '13 12:10

Alexey


3 Answers

You can override the method onOverScrolled, as it respond to the results of an over-scroll operation.

like image 160
Sean Avatar answered Nov 16 '22 12:11

Sean


Just a little more complete answer :

@Override
protected void onOverScrolled(int scrollX, int scrollY, boolean clampedX, boolean clampedY) {
    super.onOverScrolled(scrollX, scrollY, clampedX, clampedY);

    View view = (View) getChildAt(getChildCount()-1);
    int diff = (view.getBottom()-(getHeight()+getScrollY()));

    if(diff==0) {
           //overscroll on bottom
       } else {
           //overscroll on top
       }    
}
like image 35
ucMedia Avatar answered Nov 16 '22 11:11

ucMedia


scrollY = non-Zero and clampedY = true --> OverScroll state occure While Scrolling bottom to top

scrollY = Zero and clampedY = true --> OverScroll state occure While Scrolling top to bottom

so

@Override
protected void onOverScrolled(int scrollX, int scrollY, boolean clampedX, boolean clampedY) {
    super.onOverScrolled(scrollX, scrollY, clampedX, clampedY);

    if(clampedY){
        if(scrollY==0){
            //over Scroll at top
        }else {
            //over Scroll at Bottom
        }
    }
}
like image 1
Vinayak Khedkar Avatar answered Nov 16 '22 12:11

Vinayak Khedkar