Android 判断ListView滑动方向详解编程语言

代码很简单,给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)  
    { 
          
    }

原创文章,作者:ItWorker,如若转载,请注明出处:https://blog.ytso.com/10653.html

(0)
上一篇 2021年7月19日
下一篇 2021年7月19日

相关推荐

发表回复

登录后才能评论