recyclerview获取是否滑动到底部
2018-02-24 本文已影响0人
overhaha
recyclerview获取是否滑动到底部
public static boolean isVisBottom(RecyclerView recyclerView) {
LinearLayoutManager layoutManager = (LinearLayoutManager) recyclerView.getLayoutManager();
//屏幕中最后一个可见子项的position
int lastVisibleItemPosition = layoutManager.findLastVisibleItemPosition();
//当前屏幕所看到的子项个数
int visibleItemCount = layoutManager.getChildCount();
//当前RecyclerView的所有子项个数
int totalItemCount = layoutManager.getItemCount();
//RecyclerView的滑动状态
int state = recyclerView.getScrollState();
if (visibleItemCount > 0 && lastVisibleItemPosition == totalItemCount - 1 && state == recyclerView.SCROLL_STATE_IDLE) {
return true;
} else {
return false;
}
}
但是 一般需要在这里面调用
mRecyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
super.onScrollStateChanged(recyclerView, newState);
switch (newState) {
//0:当前屏幕停止滚动;1时:屏幕在滚动 且 用户仍在触碰或手指还在屏幕上;2时:随用户的操作,屏幕上产生的惯性滑动;
case RecyclerView.SCROLL_STATE_IDLE:
if (isVisBottom(mRecyclerView) == false) {
//没到底部
} else {
//滑动到底部
}
break;
case RecyclerView.SCROLL_STATE_DRAGGING:
break;
case RecyclerView.SCROLL_STATE_SETTLING:
break;
}
}
@Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
super.onScrolled(recyclerView, dx, dy);
}
});
这种方法的特点是滑动之后不能及时的判断到是否滑动到底部了