代码很简单,给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) { }
时间: 2024-10-12 03:19:28