使用recyclerview进行firebase android分页 [英] firebase android pagination with recyclerview

查看:144
本文介绍了使用recyclerview进行firebase android分页的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嘿,我正在开发一个应用程序,它将显示我每天添加到Firebase数据库中的最新数据.最新数据应出现在recyclerview的顶部.这是节点在Firebase上的样子

Hey I'm developing an app which will show latest data which I add on firebase database on daily basis. Most recent data should appear on top in recyclerview. Here is how the the nodes look like on firebase

让我们假设数据库中当前有100个节点.当应用程序打开时,它将获取数据存储在节点100到80中.当用户向下滚动时,它将获取更多的最新数据存储在节点79至59中,依此类推.

Lets assume that currently there are 100 nodes in the database. 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.

现在我可以通过此查询获取最后20个节点

Right now I'm able to get the last 20 nodes with this query

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

如果我修改查询以限制到最后21个项目,则可以在位置79处获得节点的键,但是我该如何使用第79个节点键来获取下一组节点数据,即从79到59呢?

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 i.e from 79th to 59th ?

推荐答案

您将使用endAt(),因为您希望返回的最后一个项目是您之前获得的最旧"项目.

You'd use endAt(), since you want the last item to be returned to be the "oldest" one that you got before.

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

oldestKeyYouveSeen被称为锚.您需要自己在代码中进行跟踪:将其设置为您看到过的最旧项的键.

The oldestKeyYouveSeen is known as the anchor. 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.

这篇关于使用recyclerview进行firebase android分页的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆