Firebase android pagination

后端 未结 10 1534
天命终不由人
天命终不由人 2020-11-28 09:14

I\'m building an app which will show videos stored on firebase. The list of videos needs to be paginated fetching most recent 20 videos at a time.

Here is

10条回答
  •  猫巷女王i
    2020-11-28 09:21

    I have following method to paginate through a firebase realtime database node:

    private void getUsers(String nodeId) {
            Query query;
    
            if (nodeId == null)
                query = FirebaseDatabase.getInstance().getReference()
                        .child(Consts.FIREBASE_DATABASE_LOCATION_USERS)
                        .orderByKey()
                        .limitToFirst(mPostsPerPage);
            else
                query = FirebaseDatabase.getInstance().getReference()
                        .child(Consts.FIREBASE_DATABASE_LOCATION_USERS)
                        .orderByKey()
                        .startAt(nodeId)
                        .limitToFirst(mPostsPerPage);
    
            query.addListenerForSingleValueEvent(new ValueEventListener() {
                @Override
                public void onDataChange(DataSnapshot dataSnapshot) {
                    UserModel user;
                    List userModels = new ArrayList<>();
                    for (DataSnapshot userSnapshot : dataSnapshot.getChildren()) {
                        userModels.add(userSnapshot.getValue(UserModel.class));
                    }
    
                    mAdapter.addAll(userModels);
                    mIsLoading = false;
                }
    
                @Override
                public void onCancelled(DatabaseError databaseError) {
                    mIsLoading = false;
                }
            });
        }
    

    Every time I reach the bottom of the paginated data, I call the getUsers(mAdapter.getLastItemId()); and then it brings the next set of records.

    I have written a complete guide with open source sample app on this that you can check at https://blog.shajeelafzal.com/2017/12/13/firebase-realtime-database-pagination-guide-using-recyclerview/

提交回复
热议问题