Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Paging Library loadAfter() not called [duplicate]

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.

I have an ArticlesRepository class that returns an ArticleList class containing instances of ArticleListItem 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 ArticleList with the page property set to 1 and the articles property containing a List<ArticleListItem> of articles on the requested page. I donʼt know how many articles can be on one page.

Now, I was able to implement a PageKeyedDataSource<Integer, ArticleListItem>, but it only fetches the first page:

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

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

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

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

In my ViewModel, I configure the PagedList like this:

PagedList.Config config = new PagedList.Config.Builder()
            .setEnablePlaceholders(false)
            .setInitialLoadSizeHint(1)
            .setPageSize(1)
            .setPrefetchDistance(1)
            .build();

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 PageKeyedDataSource.loadAfter method is not invoked.

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.

like image 693
Jakub Avatar asked Jul 05 '18 18:07

Jakub


Video Answer


1 Answers

The paging library should know automatically when to load new items. The problem in your implementation is that the paged RecyclerView is inside a NestedScrollView and according to this issue the libary doesn't have built in support for that.

when you put recyclerview inside an infinite scrolling parent, it will layout all of its children because the parent provides infinite dimensions.

You'll need to create your own implementation of Nested Scroll View, there is actually one here in this gist that might be able to help you.

It is also suggested to add fillViewPort to this custom nested scroll view:

android:fillViewport="true" to scrollable container

like image 67
Levi Moreira Avatar answered Oct 19 '22 18:10

Levi Moreira