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

Alex Mamo
  • 91,677
  • 13
  • 105
  • 138
Sudhakar R
  • 527
  • 6
  • 15
  • Have you solved this issue? – Joan P. May 29 '18 at 20:04
  • Well what I did was I found it was not possible to use the FirebaseRecylerAdapter I instead used this https://github.com/Malik333/RecyclerviewFirestorePagination/tree/master/app/src/androidTest/java/com/malikbisic/recyclerviewfirestorepagination and this really worked. – Sudhakar R Jun 07 '18 at 15:04
  • Thanks for your response. In the meanwhile, I have also asked a question and I got this [answer](https://stackoverflow.com/questions/50592325/is-there-a-way-to-paginate-queries-by-combining-query-cursors-using-firestorerec/50596715). Maybe it will be helpful in the future. – Joan P. Jun 07 '18 at 15:11
  • https://github.com/Malik333/RecyclerviewFirestorePagination/tree/master/app/src/androidTest/java/com/malikbisic/recyclerviewfirestorepagination was the solution that I finally used. – Sudhakar R Nov 02 '19 at 01:52
  • https://medium.com/firebase-developers/firestore-pagination-in-android-using-firebaseui-library-1d7fe1a75704 – Abdul Wahid Jan 16 '21 at 07:56
  • I think you might be interested in this article, [How to paginate Firestore using Paging 3 on Android?](https://medium.com/firebase-tips-tricks/how-to-paginate-firestore-using-paging-3-on-android-c485acb0a2df). – Alex Mamo Jan 25 '21 at 18:34

1 Answers1

-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