skip to Main Content

Hey there I only want to load the first 2 items from the Firebase Realtime database to my recycler view but the problem is it returns only 2 images and I want to load the first 2 images out of ‘n’ no. of image (for ex. return the first 2 images out of ‘n’ then scrolling down return 2 new images (like that in Instagram) and not download all images offscreen and show it on scrolling). Here is the code but I get only 2 fixed items. Help!

    public int getItemCount() {
            int size = mClicks.size();
            
          return (size > 2 ? 2 : size);
    }

2

Answers


  1. How to load the first n items in RecyclerView every time scrolling down?

    What you’re looking for is called pagination. This means that you need to load the data progressively in smaller chunks. This was already covered a lot of times before, and this question already has a lot of solutions for the Realtime Database:

    However, if you consider at some point in time trying using Cloud Firestore, I think that this answer will help:

    If you want to go further and try using Jetpack Compose, then I think that this resource will help:

    How to implement pagination in Firestore using Jetpack Compose?

    Login or Signup to reply.
  2. You should use Integer.MAX_VALUE. Something like this.

     public int getItemCount() {            
          return Integer.MAX_VALUE;
    }
    

    or simply use

    public int getItemCount() {            
          return itemList.size();
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search