I have a query that looks like this:
Query first = ref.orderBy("name", Query.Direction.ASCENDING).limit(10);
This is how I display the data to my RecyclerView
.
firestoreRecyclerOptions = new FirestoreRecyclerOptions.Builder<ModelClass>().setQuery(query, ModelClass.class).build();
myFirestoreRecyclerAdapter = new MyFirestoreRecyclerAdapter(firestoreRecyclerOptions);
recyclerView.setAdapter(myFirestoreRecyclerAdapter);
I'm trying to use pagination as specified in here and I cannot solve it.
first.get().addOnSuccessListener(new OnSuccessListener<QuerySnapshot>() {
@Override
public void onSuccess(QuerySnapshot documentSnapshots) {
DocumentSnapshot lastVisible = documentSnapshots.getDocuments().get(documentSnapshots.size() - 1);
Query second = ref.orderBy("name", Query.Direction.ASCENDING).startAfter(lastVisible).limit(10);
//Create a firestoreRecyclerOptions and setting the adapter
}
});
Is there a way to paginate queries by combining query cursors using FirestoreRecyclerAdapter
? Is this even possible?
A possible approach is save the push keys of each item in a separate node.
Then fetch all keys from that node to an array list or something ..
Then based on your pagination number fetch the keys from this array List and use orderByKey Query and startAt and LimitToFirst queries combined to make the pagination algorithm
As @FrankvanPuffelen already answered in an earlier question of yours, you cannot achieve that because in your case, you should pass 2 different queries (
first
andsecond
) to a single adapter, which is not possible withFirestoreRecyclerAdapter
. You can either use the first query or the second with a single instance of your adapter.A solution would be to create two different lists, containing the results from each query and combine them. Then you can pass the resulting list to another adapter, let's say an
ArrayAdapter
and then display the results into aListView
or even better in aRecyclerView
. The problem in this case is that you will not be able to use the real-time features that theFirestoreRecyclerAdapter
class provides but this approach will solve your problem.Edit:
According to your request from the comment section, I'll give you an example on how to paginate a query on button click in the easiest way, using a
ListView
and anArrayAdapter
. You can achieve the same thing also using aRecyclerView
when scrolling down. But to keep things simple, let's assume we have aListView
and aButton
and we want to load more items to the list on every button click. For that, let's define first the views :Let's assume we have a database structure that look like this:
And a model class that looks like this:
Now, let's define a query with the limit set to
3
.This means that on every button click, we'll load 3 more items. And now, here is the code that does the magic:
In which
lastVisible
is aDocumentSnapshot
object that represents the last visibile item from the query. In this case, every third one and it is declared as gloabl variable:Edit2: Here you have also the solution on how you can get the data from your Firestore database and display it in smaller chunks in a
RecyclerView
when user scrolls.