Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

showing a progress bar with android paging library

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?

like image 761
M.Ali Avatar asked Jun 23 '18 17:06

M.Ali


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.

Does Android use paging?

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.

What is secondary progress android?

The primary progress is the current playing time of the video, while the secondary progress is the downloaded part of the video.


1 Answers

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

like image 154
Keivan Esbati Avatar answered Nov 15 '22 00:11

Keivan Esbati