RecyclerView加载更多监听

作者: Wang_Yi | 来源:发表于2018-01-14 17:55 被阅读53次
    public abstract class OnLoadMoreListener extends RecyclerView.OnScrollListener {
        private boolean mIsScrollToBottom;
    
        private boolean mLoading;
    
        @Override
        public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
            super.onScrolled(recyclerView, dx, dy);
            mIsScrollToBottom = dy > 0;
        }
    
        @Override
        public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
            super.onScrollStateChanged(recyclerView, newState);
            if (newState != RecyclerView.SCROLL_STATE_IDLE) {
                return;
            }
    
            if (!(recyclerView.getLayoutManager() instanceof LinearLayoutManager)) {
                return;
            }
    
            LinearLayoutManager layoutManager = (LinearLayoutManager) recyclerView.getLayoutManager();
            int lastPosition = layoutManager.findLastCompletelyVisibleItemPosition();
            int itemCount = layoutManager.getItemCount();
            if (!mLoading && itemCount > 0 && mIsScrollToBottom && lastPosition == itemCount - 1) {
                onLoadMore();
                mLoading = true;
            }
        }
    
        public abstract void onLoadMore();
    
        public void onLoadingFinish() {
            this.mLoading = false;
        }
    }
    

    相关文章

      网友评论

        本文标题:RecyclerView加载更多监听

        本文链接:https://www.haomeiwen.com/subject/motloxtx.html