I read Firestore documentation and all articles on internet(stackoverflow) about Firestore pagination but no luck. I tried to implement the exact code in docs, but nothing happens. I have a basic database with items(over 1250 or more) and I want to get them progressively. By scrolling to load 15 items (to the last item in the database).
If using docs code:
// 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 to do? Documentation has not too many details.
PS: I need with recycler view (not list view) when user scrolls. Thanks
FirebaseUI-Android also recently came out with a Firestore Paginator.
I have used it in my code, and it works great - just keep in mind that it operates using .get() instead of .addSnapshotListener(), so the recycler is not in realtime.
See the docs here:
https://github.com/firebase/FirebaseUI-Android/tree/master/firestore#using-the-firestorepagingadapter
As it is mentioned in the official documentation, the key for solving this problem is to use the startAfter() method. So you can paginate queries by combining query cursors with the
limit()
method. You'll be able to use the last document in a batch as the start of a cursor for the next batch.To solve this pagination problem, please see my answer from this post, in which I have explained step by step, how you can load data from a Cloud Firestore database in smaller chunks and display it in a
ListView
on button click.Solution:
To get the data from your Firestore database and display it in smaller chunks in a
RecyclerView
, please follow the steps below.Let's take the above example in which I have used products. You can use products, cities or whatever you want. The principles are the same. Assuming that you want to load more products when user scrolls, I'll use
RecyclerView.OnScrollListener
.Let's define first the
RecyclerView
and set the layout manager:Let's assume we have a database structure that look like this:
And a model class that looks like this:
This how the adapter class should look like:
The
item_product
layout contains only one view, aTextView
.And this is the how the holder class should look like:
Now, let's define a limit as a global variable and set it to
15
.Let's define now the query using this limit:
Here is the code that also does the magic in your case:
In which
lastVisible
is aDocumentSnapshot
object which represents the last visibile item from the query. In this case, every 15'th one and it is declared as gloabl variable:And
isScrolling
andisLastItemReached
are also global variables and are declared as: