Pagination in endless recycler view with firebase

前端 未结 3 1180
清酒与你
清酒与你 2021-02-02 04:15

I am working on Q/A app . I have successfully loaded questions from firebase . But I am not able to apply pagination from Firebase like database . And how to recognize that we h

3条回答
  •  天涯浪人
    2021-02-02 04:48

    To check whether you have reached the bottom of the RecyclerView, you can use the onScrolled listener as below, the if condition here is important and it is defining when a user has reached to the bottom.

    mRV.addOnScrollListener(new RecyclerView.OnScrollListener() {
    
                @Override
                public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                    super.onScrolled(recyclerView, dx, dy);
    
                    mTotalItemCount = mLayoutManager.getItemCount();
                    mLastVisibleItemPosition = mLayoutManager.findLastVisibleItemPosition();
    
                    if (!mIsLoading && mTotalItemCount <= (mLastVisibleItemPosition + mPostsPerPage)) {
                        getUsers(mAdapter.getLastItemId());
                        mIsLoading = true;
                    }
                }
            });
    

    Secondly, you can use startAt and limitToFirst methods to get questions in batches as shown below:

    query = FirebaseDatabase.getInstance().getReference()
                        .child(Consts.FIREBASE_DATABASE_LOCATION_USERS)
                        .orderByKey()
                        .startAt(nodeId)
                        .limitToFirst(mPostsPerPage);
    

    I have created an open source app that shows exactly how it is done. Please have a look: https://blog.shajeelafzal.com/2017/12/13/firebase-realtime-database-pagination-guide-using-recyclerview/

提交回复
热议问题