Firebase Android分页问题

问题描述 投票:0回答:1

我正在构建一个应用,该应用将显示存储在Firebase上的帖子。需要对帖子列表进行分页,一次获取最近的10个帖子。

image firebase database

这里是加载帖子的方法:

private void readsposts(){

        DatabaseReference reference = FirebaseDatabase.getInstance().getReference("Posts");
        reference.keepSynced(true);

        reference.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {

                     postList.clear();

                for(DataSnapshot snapshot:dataSnapshot.getChildren()){

                    Post post = snapshot.getValue(Post.class);
                    for(String id:followingList){

                        if(post.getPublisher()!=null && post.getPublisher().equals(id)){
                            postList.add(post);
                            indication.stopShimmer();
                            indication.setVisibility(View.GONE);
                        }
                    }

                    if(post.getPublisher()!=null && post.getPublisher().equals(firebaseUser.getUid())){
                        postList.add(post);
                        //stop shimmer
                        indication.setVisibility(View.GONE);
                        indication.stopShimmer();
                     }
                }
                postAdapter.notifyDataSetChanged();

            }

            @Override
            public void onCancelled(DatabaseError databaseError) {

            }
        });
}

但是以上代码将检索整个帖子列表,而不是有限的帖子。如何实现分页?

android firebase firebase-realtime-database android-recyclerview recycler-adapter
1个回答
0
投票

when you fetch data at a location in your database, you also retrieve all of its child nodes.

(因为您阅读了“帖子”)

因此,如果您提前记录您的发布密钥,否则将下载数据

因此您可以新建一个目录以存储您的帖子密钥

© www.soinside.com 2019 - 2024. All rights reserved.