Firestore Pagination with FirestoreRecyclerAdapter (Android) [duplicate]
Asked Answered
A

1

7

I have reviewed the docs at https://firebase.google.com/docs/firestore/query-data/query-cursors

I would like to get firestore pagination working with FirestoreRecyclerAdapter.

Does anyone have a sample working code for this usecase? I have a list and that list can be potentially long. I want to set a limit and follow the strategy to paginate queries by combining query cursors with the limit() method.

Thus far this is what I have in my ListActivity:

     // Construct query for first 25 teachers, ordered by firstName
    firstQuery = FirebaseFirestore.getInstance()
            .collection("School").document(user.getUid())
            .collection("teachers")
            .orderBy("firstName")
            .limit(25);


    FirestoreRecyclerOptions<Teacher> options = new FirestoreRecyclerOptions.Builder<Teacher>()
            .setQuery(firstQuery, Teacher.class)
            .build();

    adapter = new FirestoreRecyclerAdapter<Teacher, TeacherHolder>(options) {
        @Override
        public void onBindViewHolder(final TeacherHolder holder, final int position, Teacher teacherItem) {
            // Bind the Teacher object to the TeacherHolder
            //progressBar.setVisibility(View.GONE);
            ....



        }

To implement the strategy given by firestore docs, how do I go about this the next step.

// Construct query for first 25 cities, ordered by population
Query first = db.collection("cities")
    .orderBy("population")
    .limit(25);

first.get()
.addOnSuccessListener(new OnSuccessListener<QuerySnapshot>() {
    @Override
    public void onSuccess(QuerySnapshot documentSnapshots) {
        // ...

        // Get the last visible document
        DocumentSnapshot lastVisible = documentSnapshots.getDocuments()
                .get(documentSnapshots.size() -1);

        // Construct a new query starting at this document,
        // get the next 25 cities.
        Query next = db.collection("cities")
                .orderBy("population")
                .startAfter(lastVisible)
                .limit(25);

        // Use the query for pagination
        // ...
    }
});

How do I wire the above recommendation given by firestore into my app while using the FirestoreRecyclerAdapter. Do I add a scrollListener to adapter above? and listen to the scroll events, re-run the query. A sample code that ties all this together will help me clear the wiring needed to get this all done.

I did look at some of the other chats around this topic and the closest I found was https://github.com/Malik333/RecyclerviewFirestorePagination but this doesn't use the FirestoreRecyclerAdapter which i want to use,.

Firestore doc that discusses the recycler adapter https://github.com/firebase/FirebaseUI-Android/tree/master/firestore#using-the-firestorerecycleradapter

(But not much info on how to connect this with pagination).

I was thinking perhaps I need to do something similar to this

but Looking for integration with FirestoreRecyclerAdapter code.

I was considering starting of with

myRecyclerView.setOnScrollChangeListener(new EndlessScrollListener() {
            @Override
            public boolean onLoadMore(int page, int totalItemsCount) {
                // Triggered only when new data needs to be appended to the list
                // Add whatever code is needed to append new items to your AdapterView
                //loadNextDataFromApi(page);
                // or loadNextDataFromApi(totalItemsCount);
                return true; // ONLY if more data is actually being loaded; false otherwise.
            }
        });

and following the steps outlined in this tutorial https://github.com/codepath/android_guides/wiki/Endless-Scrolling-with-AdapterViews-and-RecyclerView

Alvy answered 5/4, 2018 at 19:53 Comment(5)
Have you solved this issue?Newtonnext
Well what I did was I found it was not possible to use the FirebaseRecylerAdapter I instead used this github.com/Malik333/RecyclerviewFirestorePagination/tree/master/… and this really worked.Alvy
Thanks for your response. In the meanwhile, I have also asked a question and I got this answer. Maybe it will be helpful in the future.Newtonnext
github.com/Malik333/RecyclerviewFirestorePagination/tree/master/… was the solution that I finally used.Alvy
medium.com/firebase-developers/…Caseation
A
-1

You can do that. It’s simple, instead of using FirestoreRecyclerAdapter. You just need to use FirestorePagingAdapter. It also supports firestoreUi.

PagedList.Config config = new PagedList.Config.Builder()
    .setEnablePlaceholders(false)
    .setPrefetchDistance(10)
    .setPageSize(20)
    .build();

By using this code, you can define how many items to load when first load occurs, and also the number of items loaded at once from the DataSource. That’s it. You can know more from here

There is a slight difference between FirestoreRecyclerAdapter and FirestorePagingAdapter

Addax answered 10/4, 2019 at 11:14 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.