How does Android Paging Library know to load more data? : <p>Iʼm fairly new to developing Android apps and Iʼm trying to do everything “the right way.” So right now, Iʼm implementing the new Android Paging Library into my project, where I need to load a list of articles from a network server.</p>

<p>I have an <code>ArticlesRepository</code> class that returns an <code>ArticleList</code> class containing instances of <code>ArticleListItem</code> that I would like to display in a RecyclerView. The list of articles is paginated already on the server, so the repository sends a request for the first page and returns an <code>ArticleList</code> with the <code>page</code> property set to <code>1</code> and the <code>articles</code> property containing a <code>List&lt;ArticleListItem&gt;</code> of articles on the requested page. I donʼt know how many articles can be on one page.</p>

<p>Now, I was able to implement a <code>PageKeyedDataSource&lt;Integer, ArticleListItem&gt;</code>, but it only fetches the first page:</p>

<pre><code>@Override
public void loadInitial(@NonNull LoadInitialParams&lt;Integer&gt; params, @NonNull LoadInitialCallback&lt;Integer, ArticleListItem&gt; callback) {
    ArticleList list = load(1);
    if (list != null) {
        callback.onResult(list.articles, null, next(list));
    }
}

@Override
public void loadBefore(@NonNull LoadParams&lt;Integer&gt; params, @NonNull LoadCallback&lt;Integer, ArticleListItem&gt; callback) {
    ArticleList list = load(previous(params.key));
    if (list != null) {
        callback.onResult(list.articles, previous(list));
    }
}

@Override
public void loadAfter(@NonNull LoadParams&lt;Integer&gt; params, @NonNull LoadCallback&lt;Integer, ArticleListItem&gt; callback) {
    ArticleList list = load(next(params.key));
    if (list != null) {
        callback.onResult(list.articles, next(list));
    }
}
</code></pre>

<p>The <code>previous</code>/<code>next</code> functions return an <code>Integer</code> with the previous/next page number or <code>null</code> if there isnʼt one.</p>

<p>In my ViewModel, I configure the PagedList like this:</p>

<pre><code>PagedList.Config config = new PagedList.Config.Builder()
            .setEnablePlaceholders(false)
            .setInitialLoadSizeHint(1)
            .setPageSize(1)
            .setPrefetchDistance(1)
            .build();
</code></pre>

<p>This way Iʼm able to load the first page, but when I scroll to the bottom of the RecyclerView (that is inside a NestedScrollView), nothing happens. Debugging shows that the <code>PageKeyedDataSource.loadAfter</code> method is not invoked.</p>

<p>Do I have to somehow tell the PagedList that the next page has to be loaded, or is it the RecyclerView/DataSource/GodKnowsWhatElseʼs job and Iʼm just doing something wrong? Thanks for any advice.</p>
