Home » Java » Firebase database pagination scrolling action

Firebase database pagination scrolling action

Posted by: admin September 21, 2018 Leave a comment

Questions:

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.

Answers: