问题
I am making a image posting app in which I want to fetch first five records and then when the user scrolls down to bottom next 5 records should be fetched automatically . I am ordering by "negative_time stamp" so that the latest post comes at the top . After fetching first 5 records from bottom i want the next five records . Please help . Thanks in Advance;
I am using this query for fetching next five records
ref_to_all_posts.orderByChild("negative_timestamp").startAt( neg_timeStamp).limitToFirst(5)
回答1:
You'll need to store the timestamp
and the key of the last item of the page. Then you can get the next page with:
ref_to_all_posts
.orderByChild("negative_timestamp")
.startAt(lastTimeStamp, lastKey)
.limitToFirst(6)
Two things to note here:
- I pass in the
timestamp
and the key of the last item on the previous page. The Firebase Database uses these to find the item as the starting point for this query. - We request 6 items, since the query will also return the same item. There is no way to prevent returning this overlapping item, so you have to filter it out client-side.
来源:https://stackoverflow.com/questions/45117438/firebase-pagination-in-android