andrewedgar
andrewedgar

Reputation: 877

How to download more Firebase objects on RecyclerView Scroll?

I have an app that features chat rooms. Every room has an ID that serves as a database reference, and messages are brought down from there and displayed in a RecyclerView. I know how to increase the number of messages that are downloaded onCreate/onStart by using Query.orderbykey().limittolast(), but how do I download and display additional items when the user scrolls to the top of the RecyclerView of the chat activity, a la Facebook messenger?

Edit: Here is my adapter's construct0r:

public ChatRecyclerViewAdapter(Context mContext, ArrayList<String> mMessage, ArrayList<String> mAuthor, String mRoomID, DatabaseReference reference) {
    this.mContext = mContext;
    this.mRoomID = mRoomID;
    numberOfRecentMessages=20;

    messageList = new ArrayList<>();
    mDatabaseReference = reference.child(mRoomID+"_messages");
    recentMessages = mDatabaseReference.orderByKey().endAt(100).limitToLast(numberOfRecentMessages);
    recentMessages.addChildEventListener(mListener);

}

Upvotes: 0

Views: 75

Answers (1)

Frank van Puffelen
Frank van Puffelen

Reputation: 598740

It sounds like you're looking for endAt(), which takes the key of the last item to return. Say you you're currently showing these keys:

key20
key21
key22
key23
key24
key25
key26
key27
key28
key29

Then you can get the previous items with:

ref.orderByKey().endAt("key20").limitToLast(11)

Upvotes: 1

Related Questions