Android--判断listview上下滑动的方法

男儿欲遂平生志,五经勤向窗前读。这篇文章主要讲述Android--判断listview上下滑动的方法相关的知识,希望能为你提供帮助。

1elv_music_res_fragment.setOnScrollListener(new AbsListView.OnScrollListener() { 2private int mLastFirstPostion = 0; 3private int mLastFirstTop = 0; 4private int touchSlop = ViewConfiguration.get(getActivity()).getScaledTouchSlop(); 5 6@Override 7public void onScrollStateChanged(AbsListView view, int scrollState) { 8 9} 10 11@Override 12public void onScroll(AbsListView absListView, int firstVisibleItem, int visibleItemCount, int totalItemCount) { 13int currentTop; 14 15View firstChildView = absListView.getChildAt(0); 16if (firstChildView != null) { 17currentTop = absListView.getChildAt(0).getTop(); 18} else { 19//ListView初始化的时候会回调onScroll方法,此时getChildAt(0)仍是为空的 20return; 21} 22//判断上次可见的第一个位置和这次可见的第一个位置 23if (firstVisibleItem != mLastFirstPostion) { 24//不是同一个位置 25if (firstVisibleItem > mLastFirstPostion) { 26// 下 27if (rg_container_resFragment.getVisibility() != View.GONE) { 28rg_container_resFragment.setVisibility(View.GONE); 29} 30} else { 31// 上 32if (rg_container_resFragment.getVisibility() != View.VISIBLE) { 33rg_container_resFragment.setVisibility(View.VISIBLE); 34} 35} 36mLastFirstTop = currentTop; 37} else { 38//是同一个位置 39if (Math.abs(currentTop - mLastFirstTop) > touchSlop) { 40//避免动作执行太频繁或误触,加入touchSlop判断,具体值可进行调整 41if (currentTop > mLastFirstTop) { 42// 上 43if (rg_container_resFragment.getVisibility() != View.VISIBLE) { 44rg_container_resFragment.setVisibility(View.VISIBLE); 45} 46} else if (currentTop < mLastFirstTop) { 47// 下 48if (rg_container_resFragment.getVisibility() != View.GONE) { 49rg_container_resFragment.setVisibility(View.GONE); 50} 51} 52mLastFirstTop = currentTop; 53} 54} 55mLastFirstPostion = firstVisibleItem; 56} 57});

【Android--判断listview上下滑动的方法】 

    推荐阅读