RecyclerView判断是否滑动到底

来源:互联网 发布:php接口压力测试工具 编辑:程序博客网 时间:2024/06/05 15:46

用recyclerview实现分页加载需要调用recyclerView的addOnScrollListener方法,在重写的onScrollStateChanged方法中的监听列表是否到达底部:

@Override
public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
super.onScrollStateChanged(recyclerView, newState);
if (newState == RecyclerView.SCROLL_STATE_IDLE) {
//得到当前显示的最后一个item的view
View lastChildView = recyclerView.getLayoutManager().getChildAt(recyclerView.getLayoutManager().
getChildCount() - 1);
if (lastChildView != null) {
//得到lastChildView的bottom的坐标值
int lastChildBottom = lastChildView.getBottom();
//得到recyclerview的底部坐标减去底部padding值,也就是显示内容最底部的坐标
int recyclerBottom = recyclerView.getBottom() - recyclerView.getPaddingBottom();
//通过lastChildView得到这个view当前的position值
int lastPosition = recyclerView.getLayoutManager().getPosition(lastChildView);
//判断lastChildView的bottom值跟recyclerview的bottom,判断lastPosition是不是最后一个position
//如果条件都满足则说明真正滑动到了最底部

if (lastChildBottom==recyclerBottom&&lastPosition == recyclerView.getLayoutManager().getItemCount() - 1) {
pageIndex++;

                    }                }            }        }
0 0