Home:ALL Converter>How to add pagination in data retreived from FIREBASE STORAGE in android?

How to add pagination in data retreived from FIREBASE STORAGE in android?

Ask Time:2022-04-22T15:19:59         Author:D3mon

Json Formatter

I'm trying to add pagination in images data that I'm retrieving from firebase using Firebase Storage. I have 10 images there and I want to display 2 at a time in RecyclerView and when the user scrolls down to end vertically, it loads the next 2 until all the images are displayed, I have also read some documentation of Firebase where it was mentioned to use storage.list(int max results) method but with that, it only shows the number of results that I pass in the method for instance if I pass 2 it shows 2 images only, and I can't load anymore. I've found one method too on the official documentation i.e below: https://firebase.google.com/docs/storage/android/list-files

public void listAllPaginated(@Nullable String pageToken) {
    FirebaseStorage storage = FirebaseStorage.getInstance();
    StorageReference listRef = storage.getReference().child("files/uid");

    // Fetch the next page of results, using the pageToken if we have one.
    Task<ListResult> listPageTask = pageToken != null
            ? listRef.list(100, pageToken)
            : listRef.list(100);

    listPageTask
            .addOnSuccessListener(new OnSuccessListener<ListResult>() {
                @Override
                public void onSuccess(ListResult listResult) {
                    List<StorageReference> prefixes = listResult.getPrefixes();
                    List<StorageReference> items = listResult.getItems();

                    // Process page of results
                    // ...

                    // Recurse onto next page
                    if (listResult.getPageToken() != null) {
                        listAllPaginated(listResult.getPageToken());
                    }
                }
            }).addOnFailureListener(new OnFailureListener() {
                @Override
                public void onFailure(@NonNull Exception e) {
                    // Uh-oh, an error occurred.
                }
            });
}

I'm confused about how to use it, I don't know where I can get a page token from in order to provide a reference to open the next page

Author:D3mon,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/71964843/how-to-add-pagination-in-data-retreived-from-firebase-storage-in-android
yy