简体   繁体   English

使用recyclerview的firebase android分页

[英]firebase android pagination with recyclerview

Hey I'm developing an app which will show latest data which I add on firebase database on daily basis. 嘿我正在开发一个应用程序,它将显示我每天在firebase数据库上添加的最新数据。 Most recent data should appear on top in recyclerview. 最新数据应显示在recyclerview的顶部。 Here is how the the nodes look like on firebase 以下是firebase上节点的外观

在此输入图像描述

Lets assume that currently there are 100 nodes in the database. 让我们假设目前数据库中有100个节点。 When the app opens it gets the data stores in nodes 100 to 80. When the user scrolls down it gets more latest data stored in node 79 to 59 and so on. 当app打开时,它获得节点100到80中的数据存储。当用户向下滚动时,它获得存储在节点79到59中的更多最新数据,依此类推。

Right now I'm able to get the last 20 nodes with this query 现在我可以通过此查询获得最后20个节点

   Query jokesQuery = FirebaseDatabase.getInstance().getReference().child("hindi-jokes").orderByKey().limitToLast(20);

I can get the key of the node at position 79 if I modify the query to limit to last 21 items but how do I then use the 79th nodes key to get the next set of node data ie from 79th to 59th ? 如果我修改查询以限制最后21个项目,我可以获得位置79处的节点的密钥但是如何使用第79个节点密钥来获取下一组节点数据,即从第79个到第59个?

You'd use endAt() , since you want the last item to be returned to be the "oldest" one that you got before. 您将使用endAt() ,因为您希望将最后一项返回为您之前获得的“最旧”项。

DatabaseReference jokesRef = FirebaseDatabase.getInstance().getReference().child("hindi-jokes");
Query jokesQuery = jokesRef.orderByKey().endAt(oldestKeyYouveSeen).limitToLast(20);

The oldestKeyYouveSeen is known as the anchor. oldestKeyYouveSeen被称为锚。 You need to track this yourself in your code: setting it to the key of the oldest item that you'd you've seen. 您需要在代码中自己跟踪:将其设置为您看过的最旧项目的键。

Note that it will be both in the first query and in the second, so you'll have to explicitly exclude it once in your code. 请注意,它将在第一个查询和第二个查询中,因此您必须在代码中明确地将其排除一次。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM