Firebase pagination in Android

主宰稳场 提交于 2021-01-27 20:24:40

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!