Android 判断ListView滑动方向

清华大佬耗费三个月吐血整理的几百G的资源,免费分享!....>>>

代码很简单,给mListView监听onScrollListener事件,然后在onScroll进行判断

//listView中第一项的索引
    private int mListViewFirstItem = 0;
    //listView中第一项的在屏幕中的位置
    private int mScreenY = 0;
    //是否向上滚动
    private boolean mIsScrollToUp = false;
     
    @Override
    public void onScroll(AbsListView absListView, int firstVisibleItem,int visibleItemCount, int totalItemCount) {
     
        if(mListView.getChildCount()>0)
        {
            boolean isScrollToUp = false;
            View childAt = mListView.getChildAt(firstVisibleItem);
            int[] location = new int[2];
            childAt.getLocationOnScreen(location);
            Log.d("onScroll", "firstVisibleItem= "+firstVisibleItem+" , y="+location[1]);
             
            if(firstVisibleItem!=mListViewFirstItem)
            {
                if(firstVisibleItem>mListViewFirstItem)
                {
                    Log.e("--->", "向上滑动");
                    isScrollToUp = true;
                }else{
                    Log.e("--->", "向下滑动");
                    isScrollToUp = false;
                }
                mListViewFirstItem = firstVisibleItem;
                mScreenY = location[1];
            }else{
                if(mScreenY>location[1])
                {
                    Log.i("--->", "->向上滑动");
                    isScrollToUp = true;
                }
                else if(mScreenY<location[1])
                {
                    Log.i("--->", "->向下滑动");
                    isScrollToUp = false;
                }
                mScreenY = location[1];
            }
             
            if(mIsScrollToUp!=isScrollToUp)
            {
                onScrollDirectionChanged(mIsScrollToUp);
            }
             
        }
    }
    private void onScrollDirectionChanged(boolean isScrollToUp) 
    {
         
    }