Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does Android Paging Library know to load more data?

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 521
Jakub Avatar asked Jul 05 '18 18:07

Jakub


People also ask

What is Paging library in Android?

The Paging Library lets you load data directly from your backend using keys that the network provides. Your data can be uncountably large. Using the Paging Library, you can load data into pages until there isn't any data remaining. You can observe your data more easily.

What is jetpack Paging?

Paging library overview Part of Android Jetpack. The Paging library helps you load and display pages of data from a larger dataset from local storage or over network. This approach allows your app to use both network bandwidth and system resources more efficiently.

Which API defines the source of data to be retrieved from a single source in Paging?

PagingSource object defines a source of data and how to retrieve data from that source. A PagingSource object can load data from any single source, including network sources and local databases.

How can I get data from PagingData?

Display the paged data in your UICreate an instance of your PagingDataAdapter class. Pass the PagingDataAdapter instance to the RecyclerView list that you want to display your paged data. Observe the PagingData stream, and pass each generated value to your adapter's submitData() method.

What is paging library in Android?

The paging library supports both large bounded list and unbounded lists, such as continuously updating feeds. The paging library is based on the idea of sending lists to the UI with the live data of a list that is observed by RecyclerView.Adapter.

What is paging and how does it work?

The Paging library helps you load and display pages of data from a larger dataset from local storage or over network. This approach allows your app to use both network bandwidth and system resources more efficiently.

What are the paging library components in a repository?

The primary Paging library component in the repository layer is PagingSource. Each PagingSource object defines a source of data and how to retrieve data from that source. A PagingSource object can load data from any single source, including network sources and local databases. Another Paging library component that you might use is RemoteMediator.

How do I load data from pagingsource to recyclerview?

The Pager object calls the load () method from the PagingSource object, providing it with the LoadParams object and receiving the LoadResult object in return. You also need to set up an adapter to receive the data into your RecyclerView list. The Paging library provides the PagingDataAdapter class for this purpose.


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 157
Levi Moreira Avatar answered Oct 10 '22 04:10

Levi Moreira