判断RecycleView滑动到顶部及底部

官方推出RecycleView控件用来替代ListView,其功能很是强大,今天说说如何判断RecycleView滑动到顶部及底部的功能。
我们知道使用RecycleView之前必须设置setLayoutManager(LayoutManager layout),否则不起作用。
有三种设置方式:
一:LinearLayoutManager
二:StaggeredGridLayoutManager
三:GridLayoutManager
今天不讲这三种设置,至于提到他们是因为在判断滑动到顶部及底部时会用到。

LinearLayoutManager

判断滑动到顶部及底部

public class ImRecyclerView extends RecyclerView {
       //判断滑动到底部
        public boolean canPullDown() {
            if (getLayoutManager() instanceof LinearLayoutManager){
                LinearLayoutManager linearLayoutManager = (LinearLayoutManager) getLayoutManager();
                int firstItem = linearLayoutManager.findFirstVisibleItemPosition();
                if (getChildAt(0).getY() == 0f && firstItem == 0) {
                    return true;
                } else {
                    return false;
                }
            }
            return false;
        }
    //判断滑动到顶部
    public boolean canPullUp() {
        if (getLayoutManager() instanceof LinearLayoutManager){
            LinearLayoutManager linearLayoutManager = (LinearLayoutManager) getLayoutManager();
            int firstItem = linearLayoutManager.findFirstVisibleItemPosition();
            int lastItem = linearLayoutManager.findLastVisibleItemPosition();
            int itemCount = linearLayoutManager.getItemCount();
            View lastChild = getChildAt(lastItem - firstItem);
            if ((lastItem == itemCount - 1) &&  lastChild != null && lastChild.getBottom() <= getMeasuredHeight()) {
                return true;
            }
        }
        return false;
    }
}

StaggeredGridLayoutManager

public class ImRecyclerView extends RecyclerView {
   //判断滑动到底部
    public boolean canPullDown() {
        if (getLayoutManager() instanceof StaggeredGridLayoutManager) {
            int aa[] = ((StaggeredGridLayoutManager) getLayoutManager()).findFirstVisibleItemPositions(null);
            if (getChildAt(0).getY() == 0f && aa[0] == 0) {
                return true;
            } else {
                return false;
            }
        }
        return false;
    }

    //判断滑动到顶部
    public boolean canPullUp() {
            if (getLayoutManager() instanceof StaggeredGridLayoutManager) {
            StaggeredGridLayoutManager layoutManager = (StaggeredGridLayoutManager) getLayoutManager();

            int[] firstVisibleItems =((StaggeredGridLayoutManager)getLayoutManager()).findFirstVisibleItemPositions(null);
            // 真实Position就是position - firstVisibleItems[0]
            int itemCount = layoutManager.getItemCount();
            int[] lastPositions = new int[(layoutManager).getSpanCount()];
            (layoutManager).findLastVisibleItemPositions(lastPositions);
            int lastPosition = findMax(lastPositions);
            View lastChild = getChildAt(lastPosition - firstVisibleItems[0]);
            if ((lastPosition == itemCount - 1) &&  lastChild != null && lastChild.getBottom() <= getMeasuredHeight()) {
                return true;
            }
        }
        return false;
}
}

你可能感兴趣的:(判断RecycleView滑动到顶部及底部)