1

I am trying to implement Gridlayout with firestore database and make it scroll 5 items and then another 5 items.

I have tried this code from How to paginate Firestore with Android? but doesn't work it is for linearlayout but I want gridlayout :

 private DocumentSnapshot lastVisible;
 private boolean isScrolling;
 private boolean isLastItemReached;
 private int limit = 6;

 //________RV________
 MainCategoriesRV = view.findViewById(R.id.MainCategoriesRV);
 MainCategoriesRV.setHasFixedSize(true);
 gLayoutManager=new GridLayoutManager(MainCategoriesRV.getContext(),2);

 MainCategoriesRV.setLayoutManager(gLayoutManager);

 //______PROGRESS BAR__________
 MainActivityProgress = view.findViewById(R.id.MainActivityProgress);


 final CollectionReference ref = firebaseFirestore
     .collection("MainCategories");

 Query firstQuery = ref
     .orderBy("Category_Name", Query.Direction.ASCENDING).limit(5);

 firstQuery.get().addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
      @Override
      public void onComplete(@NonNull Task<QuerySnapshot> task) {
      if ( task.isSuccessful() ) {
           final List<mainCategroies> list = new ArrayList<>();
           for (DocumentSnapshot document : task.getResult()) {
           mainCategroies maincategroies = document.toObject(mainCategroies.class);
           // assert maincategroies != null;
           // maincategroies.setId(document.getId());

           list.add(maincategroies);
           }
           final CategoriesAdapter categoriesAdapter = new CategoriesAdapter(list);
           MainCategoriesRV.setAdapter(categoriesAdapter);

           lastVisible = task.getResult().getDocuments().get(task.getResult().size() -1);

           RecyclerView.OnScrollListener onScrollListener = new RecyclerView.OnScrollListener() {
           @Override
           public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
                super.onScrollStateChanged(recyclerView, newState);
                if ( newState == AbsListView.OnScrollListener.SCROLL_STATE_TOUCH_SCROLL ) {
                isScrolling = true;
                }
           }

           @Override
           public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);
                int firstVisibleItemPosition = gLayoutManager.findFirstVisibleItemPosition();
                int visibleItemCount = gLayoutManager.getChildCount();
                int totalItemCount = gLayoutManager.getItemCount();
                if ( isScrolling && (firstVisibleItemPosition+visibleItemCount== totalItemCount) && !isLastItemReached ) {
                isScrolling = false;
                Query nextQuery = ref.orderBy("Category_Name", Query.Direction.ASCENDING)
                    .startAfter(lastVisible)
                    .limit(limit);
                nextQuery.get().addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
                @Override
                public void onComplete(@NonNull Task<QuerySnapshot> task) {
                     for (DocumentSnapshot d : task.getResult()) {
                     mainCategroies maincategroies = d.toObject(mainCategroies.class);
                     list.add(maincategroies);
                     }
                     categoriesAdapter.notifyDataSetChanged();
                     for (int i = 0; i < task.getResult().size(); i++) {
                     lastVisible = task.getResult().getDocuments().get(task.getResult().size() - 1);
                     }

                     Toast.makeText(getContext(), "please wait,... ", Toast.LENGTH_SHORT).show();
                     if ( task.getResult().size() < limit ) {
                     isLastItemReached = true;

                     }
                }
                });

                }

           }
           };

           MainCategoriesRV.addOnScrollListener(onScrollListener);

      }
      }
 });

and this is my xml files

  <RelativeLayout>
  <android.support.v7.widget.RecyclerView
    android:id="@+id/MainCategoriesRV"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentStart="true"
    android:layout_centerVertical="true">

</android.support.v7.widget.RecyclerView>

 </RelativeLayout>

But it doesn't work.I have searched alot but no obvious answer.

The problem is that only loading the 1st 5 items and no loading of the next 5 items when I try to scroll. So How can I sovle this problem?

Amr Mahmoud
  • 122
  • 1
  • 15

0 Answers0