How to detect overscroll in Android ListView?

喜夏-厌秋 提交于 2019-12-19 05:11:26

问题


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


回答1:


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




回答2:


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
       }    
}



回答3:


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
        }
    }
}


来源:https://stackoverflow.com/questions/19334323/how-to-detect-overscroll-in-android-listview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!