Android Recyclerview 监听滑动状态和滑动距离

Posted 王睿丶

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Android Recyclerview 监听滑动状态和滑动距离相关的知识,希望对你有一定的参考价值。

计算滑动距离

recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() 
            @Override
            public void onScrolled(@NonNull RecyclerView recyclerView, int dx, int dy) 
                super.onScrolled(recyclerView, dx, dy);
                Log.d(TAG,getScollYDistance()+"");
            
);
    public int getScollYDistance() 
        LinearLayoutManager layoutManager = (LinearLayoutManager) rvHomeHouse.getLayoutManager();
        int position = layoutManager.findFirstVisibleItemPosition();
        View firstVisiableChildView = layoutManager.findViewByPosition(position);
        int itemHeight = firstVisiableChildView.getHeight();
        return (position) * itemHeight - firstVisiableChildView.getTop();
    

监听滑动状态

recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() 
            @Override
            public void onScrollStateChanged(@NonNull RecyclerView recyclerView, int newState) 
                super.onScrollStateChanged(recyclerView, newState);
                switch (newState)
                    case RecyclerView.SCROLL_STATE_DRAGGING:
                        Log.d("wangrui","正在拖拽");
                        break;
                    case RecyclerView.SCROLL_STATE_IDLE:
                        Log.d("wangrui","滑动停止");
                        break;
                    case RecyclerView.SCROLL_STATE_SETTLING:
                        Log.d("wangrui","惯性滑动中");
                        break;
                
            
                        @Override
            public void onScrolled(@NonNull RecyclerView recyclerView, int dx, int dy) 
                super.onScrolled(recyclerView, dx, dy);
                /*每次滑动必定调用这里*/
            

以上是关于Android Recyclerview 监听滑动状态和滑动距离的主要内容,如果未能解决你的问题,请参考以下文章

Android Recyclerview监听滑动状态

android recyclerview滑动监听滑过多少条

Android教程2020 - RecyclerView获取滑动距离

RecyclerView滑动监听

recyclerview怎么监听滑动事件

Android短视频滑动播放(一)