1

I'm following the tutorial: https://codelabs.developers.google.com/codelabs/firebase-android/#6

I was trying to achieve pagination, Here is what i have modified:

...
Query query = databaseRef.limitToLast(50);
...
FirebaseRecyclerOptions<FriendlyMessage> options =
            new FirebaseRecyclerOptions.Builder<FriendlyMessage>()
                    .setQuery(query, parser)
                    .build();
...

Here is the scrolling code as tutorial as default:

mFirebaseAdapter.registerAdapterDataObserver(new 
RecyclerView.AdapterDataObserver() {
   @Override
   public void onItemRangeInserted(int positionStart, int itemCount) {
       super.onItemRangeInserted(positionStart, itemCount);
       int friendlyMessageCount = mFirebaseAdapter.getItemCount();
       int lastVisiblePosition =
              mLinearLayoutManager.findLastCompletelyVisibleItemPosition();
       // If the recycler view is initially being loaded or the 
       // user is at the bottom of the list, scroll to the bottom 
       // of the list to show the newly added message.
       if (lastVisiblePosition == -1 ||
               (positionStart >= (friendlyMessageCount - 1) &&
                       lastVisiblePosition == (positionStart - 1))) {
           mMessageRecyclerView.scrollToPosition(positionStart);
       }
   }
});

mMessageRecyclerView.setAdapter(mFirebaseAdapter);

Now the screen shows only 50 messages.

But it don't scroll to the bottom when new messages coming.It works fine before using query.

I want to know where should I start to modified.

Thank you.

  • [Here](https://stackoverflow.com/questions/42502024/get-the-data-from-the-firebase-in-limit-to-perform-pull-to-refresh-and-load-more) Start from lastItem when refreshing with `startAt(oldestPostId)`. Or, a complete solution for that: https://stackoverflow.com/questions/44777989/firebase-infinite-scroll-list-view-load-10-items-on-scrolling/44796538#44796538 – ʍѳђઽ૯ท Aug 18 '18 at 11:28
  • @ʍѳђઽ૯ท thanks for your comment, I have edited the question. I just found out that I need to deal with this problem before implementing the scroll up action. – asdfghj1237890 Aug 18 '18 at 12:27
  • **[This](https://stackoverflow.com/questions/50741958/how-to-paginate-firestore-with-android)** is a recommended way in which you can paginate queries by combining query cursors with the limit() method. I also recommend you take a look at this **[video](https://www.youtube.com/watch?v=KdgKvLll07s)** for a better understanding. – Alex Mamo Aug 20 '18 at 11:13

1 Answers1

0

From the swift side:

By changing the startKey, we can query the data from where we want(from the end of the database) and achieve the pagination by scrolling to the top of the screen.

if (startKey == nil){
        print("firebasetest_startkey: ",self.startKey)
        // for first grabbing data operation

        _refHandle = self.ref.child(channel_title).queryOrderedByKey().queryLimited(toLast: 30).observe(.value){(snapshot) in
            guard let children = snapshot.children.allObjects.first as? DataSnapshot else{return}
            if (snapshot.childrenCount > 0){
                for child in snapshot.children.allObjects as! [DataSnapshot]{
                    if(!(self.messageKeys.contains((child as AnyObject).key))){
                        self.messages.append(child)
                        self.messageKeys.append(child.key)
                        self.itemTable.insertRows(at: [IndexPath(row: self.messages.count-1, section: 0)], with: .automatic)
                    }
                }
                self.startKey = children.key
            }
        }
    }else if (dragdirection == 0 && startKey != nil){
        //going up
        // for all other grabbing data operation from the bottom of the database

        _refHandle = self.ref.child(channel_title).queryOrderedByKey().queryEnding(atValue: self.startKey).queryLimited(toLast: 10).observe(.value){(snapshot) in
            guard let children = snapshot.children.allObjects.first as? DataSnapshot else{return}
            if (snapshot.childrenCount > 0 ){
                for child in snapshot.children.reversed(){
                    if ((child as AnyObject).key != self.startKey &&
                        !(self.messageKeys.contains((child as AnyObject).key))){
                        self.messages.insert(child as! DataSnapshot, at:0)
                        self.messageKeys.append((child as AnyObject).key)
                        self.itemTable.insertRows(at: [IndexPath(row: 0, section: 0)], with: .fade)
                    }
                }
                self.startKey = children.key
            }
        }
    }