I'm using paging library and room, making my room db as the single layer of truth of my app. when fetching is done from the server using retrofit, i save the result locally in the db then i get the data from the db using paging library. I'm using BoundaryCallback .
@Override
public void onZeroItemsLoaded() {
requestAndSaveData();
}
@Override
public void onItemAtEndLoaded(@NonNull Photo itemAtEnd) {
requestAndSaveData();
}
private void requestAndSaveData() {
if (isRequestInProgress) return;
isRequestInProgress = true;
apiInterface.getPhotos(lastRequestPage, NETWORK_PAGE_SIZE).enqueue(new Callback<PhotoList>() {
@Override
public void onResponse(Call<PhotoList> call, Response<PhotoList> response) {
if (response.isSuccessful()) {
cache.insertPhotos(response.body().getHits()); //todo only save 20 - 40 items
lastRequestPage++;
isRequestInProgress = false;
Log.i("deb", "number from boundary: " + response.body().getHits().size());
}
}
When there is no item in the db or the user scroll to the last item i call requestAndSaveData()
method that fetch the data from the server and save it into the db.
My question is how to show a progress bar at the bottom of the list while loading the next page from the server and saving it to the db as it may take a long time?
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.
Paging library overview Part of Android Jetpack. Stay organized with collections Save and categorize content based on your preferences. The Paging library helps you load and display pages of data from a larger dataset from local storage or over network.
The primary progress is the current playing time of the video, while the secondary progress is the downloaded part of the video.
First of all you could broaden your question scope by asking about how to show a loading indicator in the bottom of RecyclerView
instead of narrowing it to the Paging Library..
Secondly, you can easily show a loading indicator in the bottom of your list by adding an extra cell to the list that is just only for showing load indicator, the similar idea is used here to show the network status:
https://github.com/googlesamples/android-architecture-components/blob/master/PagingWithNetworkSample/lib/src/main/java/com/android/example/paging/pagingwithnetwork/reddit/ui/PostsAdapter.kt
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With