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;
    }

感谢原博主:博主直达

上一篇:【死磕 Spring】----- 深入分析 ApplicationContext 的 refresh()


下一篇:Sensor Fusion 论文精读系列(二)