title: Firestore Pagination with FirestoreRecyclerAdapter (Android)
body: <p>I have reviewed the docs at <a href="https://firebase.google.com/docs/firestore/query-data/query-cursors" rel="nofollow noreferrer">https://firebase.google.com/docs/firestore/query-data/query-cursors</a></p>
  
  <p>I would like to get firestore pagination working with <code>FirestoreRecyclerAdapter</code>. </p>
  
  <p>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.  </p>
  
  <p>Thus far this is what I have in my ListActivity:</p>
  
  <pre><code>     // Construct query for first 25 teachers, ordered by firstName
      firstQuery = FirebaseFirestore.getInstance()
              .collection("School").document(user.getUid())
              .collection("teachers")
              .orderBy("firstName")
              .limit(25);
  
  
      FirestoreRecyclerOptions&lt;Teacher&gt; options = new FirestoreRecyclerOptions.Builder&lt;Teacher&gt;()
              .setQuery(firstQuery, Teacher.class)
              .build();
  
      adapter = new FirestoreRecyclerAdapter&lt;Teacher, TeacherHolder&gt;(options) {
          @Override
          public void onBindViewHolder(final TeacherHolder holder, final int position, Teacher teacherItem) {
              // Bind the Teacher object to the TeacherHolder
              //progressBar.setVisibility(View.GONE);
              ....
  
  
  
          }
  </code></pre>
  
  <p>To implement the strategy given by firestore docs, how do I go about this the next step.  </p>
  
  <pre><code>// Construct query for first 25 cities, ordered by population
  Query first = db.collection("cities")
      .orderBy("population")
      .limit(25);
  
  first.get()
  .addOnSuccessListener(new OnSuccessListener&lt;QuerySnapshot&gt;() {
      @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
          // ...
      }
  });
  </code></pre>
  
  <p>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.</p>
  
  <p>I did look at some of the other chats around this topic and the closest I found was <a href="https://github.com/Malik333/RecyclerviewFirestorePagination" rel="nofollow noreferrer">https://github.com/Malik333/RecyclerviewFirestorePagination</a> but this doesn't use the FirestoreRecyclerAdapter which i want to use,.</p>
  
  <p>Firestore doc that discusses the recycler adapter
  <a href="https://github.com/firebase/FirebaseUI-Android/tree/master/firestore#using-the-firestorerecycleradapter" rel="nofollow noreferrer">https://github.com/firebase/FirebaseUI-Android/tree/master/firestore#using-the-firestorerecycleradapter</a></p>
  
  <p>(But not much info on how to connect this with pagination).</p>
  
  <p>I was thinking perhaps I need to do something similar to <a href="https://github.com/Malik333/RecyclerviewFirestorePagination/blob/master/app/src/main/java/com/malikbisic/recyclerviewfirestorepagination/MainActivity.java" rel="nofollow noreferrer">this</a></p>
  
  <p>but Looking for integration with FirestoreRecyclerAdapter code.</p>
  
  <p>I was considering starting of with </p>
  
  <pre><code>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.
              }
          });
  </code></pre>
  
  <p>and following the steps outlined in this tutorial
  <a href="https://github.com/codepath/android_guides/wiki/Endless-Scrolling-with-AdapterViews-and-RecyclerView" rel="nofollow noreferrer">https://github.com/codepath/android_guides/wiki/Endless-Scrolling-with-AdapterViews-and-RecyclerView</a></p>