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