app/src/main/java/org/weibeld/nytexplore/listener/EndlessRecyclerViewScrollListener.java
@Override
    public void onScrolled(RecyclerView view, int dx, int dy) {
        int lastVisibleItemPosition = 0;

        // Number of items currently in the RecyclerView (before load)
        int totalItemCount = mLayoutManager.getItemCount();

        if (mLayoutManager instanceof StaggeredGridLayoutManager) {
            int[] lastVisibleItemPositions = ((StaggeredGridLayoutManager) mLayoutManager).findLastVisibleItemPositions(null);
            // get maximum element within the list
            lastVisibleItemPosition = getLastVisibleItem(lastVisibleItemPositions);
        } else if (mLayoutManager instanceof GridLayoutManager) {
            lastVisibleItemPosition = ((GridLayoutManager) mLayoutManager).findLastVisibleItemPosition();
        } else if (mLayoutManager instanceof LinearLayoutManager) {
            lastVisibleItemPosition = ((LinearLayoutManager) mLayoutManager).findLastVisibleItemPosition();
        }

        // If the total item count is zero and the previous isn't, assume the
        // list is invalidated and should be reset back to initial state
        if (totalItemCount < previousTotalItemCount) {
            this.currentPage = this.startingPage;
            this.previousTotalItemCount = totalItemCount;
            if (totalItemCount == 0) {
                this.loading = true;
            }
        }
        // If it’s still loading, we check to see if the dataset count has
        // changed, if so we conclude it has finished loading and update the current page
        // number and total item count.
        if (loading && (totalItemCount > previousTotalItemCount)) {
            loading = false;
            // Set previousTotalItemCount to number of items before load
            previousTotalItemCount = totalItemCount;
        }

        // If it isn’t currently loading, we check to see if we have breached
        // the visibleThreshold and need to reload more data.
        // If we do need to reload some more data, we execute onLoadMore to fetch the data.
        // threshold should reflect how many total columns there are too
        if (!loading && (lastVisibleItemPosition + visibleThreshold) > totalItemCount) {
            currentPage++;
            if (currentPage <= maxPage) {
                onLoadMore(currentPage, totalItemCount, view);
                loading = true;
            }
        }
    }
// but first we check if we are waiting for the previous load to finish.
but first we check if we are waiting for the previous load to finish.
but first we check if we are waiting for the previous load to finish .
@Override
    public void onScrolled(RecyclerView view, int dx, int dy) {
        int lastVisibleItemPosition = 0;
        int totalItemCount = mLayoutManager.getItemCount();
        if (mLayoutManager instanceof StaggeredGridLayoutManager) {
            int[] lastVisibleItemPositions = ((StaggeredGridLayoutManager) mLayoutManager).findLastVisibleItemPositions(null);
            lastVisibleItemPosition = getLastVisibleItem(lastVisibleItemPositions);
        } else if (mLayoutManager instanceof GridLayoutManager) {
            lastVisibleItemPosition = ((GridLayoutManager) mLayoutManager).findLastVisibleItemPosition();
        } else if (mLayoutManager instanceof LinearLayoutManager) {
            lastVisibleItemPosition = ((LinearLayoutManager) mLayoutManager).findLastVisibleItemPosition();
        }
        if (totalItemCount < previousTotalItemCount) {
            this.currentPage = this.startingPage;
            this.previousTotalItemCount = totalItemCount;
            if (totalItemCount == 0) {
                this.loading = true;
            }
        }
        if (loading && (totalItemCount > previousTotalItemCount)) {
            loading = false;
            previousTotalItemCount = totalItemCount;
        }
        if (!loading && (lastVisibleItemPosition + visibleThreshold) > totalItemCount) {
            currentPage++;
            if (currentPage <= maxPage) {
                onLoadMore(currentPage, totalItemCount, view);
                loading = true;
            }
        }
    }
@ Override public void onScrolled ( RecyclerView view , int dx , int dy ) { int lastVisibleItemPosition = 0 ; int totalItemCount = mLayoutManager . getItemCount ( ) ; if ( mLayoutManager instanceof StaggeredGridLayoutManager ) { int [ ] lastVisibleItemPositions = ( ( StaggeredGridLayoutManager ) mLayoutManager ) . findLastVisibleItemPositions ( null ) ; lastVisibleItemPosition = getLastVisibleItem ( lastVisibleItemPositions ) ; } else if ( mLayoutManager instanceof GridLayoutManager ) { lastVisibleItemPosition = ( ( GridLayoutManager ) mLayoutManager ) . findLastVisibleItemPosition ( ) ; } else if ( mLayoutManager instanceof LinearLayoutManager ) { lastVisibleItemPosition = ( ( LinearLayoutManager ) mLayoutManager ) . findLastVisibleItemPosition ( ) ; } if ( totalItemCount < previousTotalItemCount ) { this . currentPage = this . startingPage ; this . previousTotalItemCount = totalItemCount ; if ( totalItemCount == 0 ) { this . loading = true ; } } if ( loading && ( totalItemCount > previousTotalItemCount ) ) { loading = false ; previousTotalItemCount = totalItemCount ; } if ( ! loading && ( lastVisibleItemPosition + visibleThreshold ) > totalItemCount ) { currentPage ++ ; if ( currentPage <= maxPage ) { onLoadMore ( currentPage , totalItemCount , view ) ; loading = true ; } } }
but first we check if we are waiting for the previous load to finish.
but first we check if we are waiting for the previous load to finish .
// Number of items currently in the RecyclerView (before load) // get maximum element within the list // If the total item count is zero and the previous isn't, assume the // list is invalidated and should be reset back to initial state // If it’s still loading, we check to see if the dataset count has // changed, if so we conclude it has finished loading and update the current page // number and total item count. // Set previousTotalItemCount to number of items before load // If it isn’t currently loading, we check to see if we have breached // the visibleThreshold and need to reload more data. // If we do need to reload some more data, we execute onLoadMore to fetch the data. // threshold should reflect how many total columns there are too