How to use Firebase RecyclerView With Load more Pagination

Viewed 15

I Have a huge set of data on Firebase Database, and I want to create a Pagination Functionality for the App. I use try many pagination code but cannot to solve the problem.

recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
        @Override
        public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
            super.onScrollStateChanged(recyclerView, newState);

            if (!recyclerView.canScrollVertically(1) && newState==RecyclerView.SCROLL_STATE_IDLE) {
                 mCurrenPage++;
                 loadPost();

            }
        }
    });



private void loadPost() {

    DatabaseReference ref = FirebaseDatabase.getInstance().getReference("Posts");
    Query q = ref.limitToLast(mCurrenPage * TOTAL_ITEMS_TO_LOAD);
    q.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
            pg.setVisibility(View.GONE);
            postList.clear();
            for (DataSnapshot ds : dataSnapshot.getChildren()) {
                ModelPost modelPost = ds.getValue(ModelPost.class);

                postList.add(modelPost);
                adapterPost = new AdapterPost(getActivity(), postList);
                recyclerView.setAdapter(adapterPost);
                adapterPost.notifyDataSetChanged();
            }
        }

        @Override
        public void onCancelled(@NonNull DatabaseError databaseError) {

        }
    });
}

am using firebase real time database.

0 Answers
Related