Android 判断ListView滑动方向
2018-07-20 来源:open-open
代码很简单,给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) { }
标签: 代码
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点!
本站所提供的图片等素材,版权归原作者所有,如需使用,请与原作者联系。
上一篇:php的encode64编码类
最新资讯
热门推荐