RecyclerView載入更多監聽

月光邊境發表於2018-01-19
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;
    }
}
複製程式碼

相關文章