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;
}
感谢原博主:博主直达