Firebase android pagination

后端 未结 10 1531
天命终不由人
天命终不由人 2020-11-28 09:14

I\'m building an app which will show videos stored on firebase. The list of videos needs to be paginated fetching most recent 20 videos at a time.

Here is

10条回答
  •  悲&欢浪女
    2020-11-28 09:27

    In the past (but no longer) a question regarding implementing Pagination to a query on Firestore was marked as a duplicate of this question I will answer for FireStore here.

    Paginate a query on android Firestore

    Query query = db.collection("cities")
            .orderBy("population")
            .limit(25);
    
    Query next = query;
    
     private void loadCities() {
    
         query = next;
         query.get()
            .addOnSuccessListener(new OnSuccessListener() {
                @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.
                    next = db.collection("cities")
                            .orderBy("population")
                            .startAfter(lastVisible)
                            .limit(25);
    
                }
            });         
     }
    

    Now just call the loadCities() method whenever you need to load more cities.

提交回复
热议问题