其他分享
首页 > 其他分享> > Android开发之RecyclerView滑动到底部的监听方法

Android开发之RecyclerView滑动到底部的监听方法

作者:互联网

Kotlin版本

recyclerview.addOnScrollListener(object: RecyclerView.OnScrollListener() {
            override fun onScrolled(recyclerView: RecyclerView, dx: Int, dy: Int) {
                super.onScrolled(recyclerView, dx, dy)
                if (isSlideToBottom(recyclerView)) {
                    //滑动到底部了
                    Toast.makeText(context,"滑动底部",Toast.LENGTH_SHORT).show()
                }
            }
        })
    }

    private fun isSlideToBottom(rv: RecyclerView?): Boolean {
        if (rv == null) return false
        return rv.computeVerticalScrollExtent() + rv.computeVerticalScrollOffset() >= rv.computeVerticalScrollRange()
    }

再看下Java版本

recyclerview.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
                super.onScrollStateChanged(recyclerView, newState);
            }
 
            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);
                LogUtils.d("------->isSlideToBottom:" + isSlideToBottom(recyclerView));
                if (isSlideToBottom(recyclerView)) {
                    srlLayout.setEnabled(true);
                }
            }
        });



 protected boolean isSlideToBottom(RecyclerView recyclerView) {
        if (recyclerView == null) return false;
        if (recyclerView.computeVerticalScrollExtent() + recyclerView.computeVerticalScrollOffset() >= recyclerView.computeVerticalScrollRange())
            return true;
        return false;
    }

感谢原博主:博主直达

标签:isSlideToBottom,return,RecyclerView,rv,onScrolled,Android,recyclerView,监听
来源: https://blog.csdn.net/xiayiye5/article/details/121302424