0

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);
    }
Alex Mamo
  • 130,605
  • 17
  • 163
  • 193

2 Answers2

0

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?

Alex Mamo
  • 130,605
  • 17
  • 163
  • 193
  • Actually I’m not looking for pagination, I don’t want infinite scroll but to load 1 item (image posted by one user) out of all images (suppose 10) but real-time database downloads all offscreen data as well , so I don’t real-time database to fetch all image data at once but when user scrolls down . Also it should not be infinite scroll as I want to show limited images –  Sep 07 '22 at 04:05
  • To activate the new data, when the user scrolls down, is called pagination. You can do it automatically, or manually on a button click. – Alex Mamo Sep 07 '22 at 06:07
  • So, can I help you with other information regarding the initial question? – Alex Mamo Sep 07 '22 at 06:12
  • Yes please but it should not do infinite scroll. –  Sep 07 '22 at 07:48
  • If you want to scroll, and if want to load more data when you reach the limit, this is called pagination, or infinite scroll. If you don't want that, then load more data only on demand, for example, on a button click, right? – Alex Mamo Sep 07 '22 at 10:45
  • I want to load data like instagram does. When there is only one post on instagram it doesn't do infinite scroll.Does pagination includes downloading whole database to know certain limit? If yes then it's the same as downloading whole database to know certain limit , if this is true , then there is no point to show data of 'n' images out of total as I want to save Real-time data usage. –  Sep 07 '22 at 18:39
  • No, pagination doesn't need to download the entire database. It'll only download a fixed number of objects at a time. Besides that, if you want your users to be able to load only a fixed number of pages, that can be also done in your application code. – Alex Mamo Sep 07 '22 at 19:30
  • So, can I help you with other information? – Alex Mamo Sep 07 '22 at 19:30
  • I'm using a model class , adapter class and main activity class, how to implement pagination in that can you share some references? I checked the link you shared above and one user is still complaining about entire database being downloaded even after pagination. –  Sep 08 '22 at 06:52
  • Maybe such a [resource](https://www.youtube.com/watch?v=jIGxgA_wHvg) will help. Does it? – Alex Mamo Sep 08 '22 at 07:05
  • Hey Siddharth. Can I help you with other information? – Alex Mamo Sep 10 '22 at 07:01
  • 1
    I checked your channel sir , your videos are really helping! Thank you. –  Sep 10 '22 at 09:23
  • Good to hear that, Siddharth ;) – Alex Mamo Sep 10 '22 at 09:30
0

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();
}
  • I need pagination like effect by modifying int getItemCount() –  Sep 06 '22 at 11:45
  • do you mean Progress bar or loading bar? I did'nt understood "pagination like effect" – Rahul Sharma Sep 06 '22 at 12:25
  • Yes sir , I want to load latest 2 images out of 10 images every user upload like on instagram ,but recyclerview is downloading all 10 images data and loading it. I want when user scroll down after 2 images , new images of same quantity (2) should load , progress bar is optional. –  Sep 06 '22 at 13:01
  • you can use room to store the 10 data and then show it accordingly. – Rahul Sharma Sep 07 '22 at 11:17
  • It's the same as downloading whole database tho. I don't want whole database just data of 2 and everytime user scrolls bottom , the data of 2 new images will be fetch avoiding consumption of data download for rest images(here 6 left out of 10 data). –  Sep 07 '22 at 18:31
  • I have a solution: rvFoldersList.addOnScrollListener(new RecyclerView.OnScrollListener() {if (newState == RecyclerView.SCROLL_STATE_IDLE) { if (!isLoadingDialogShowing) { if (!recyclerView.canScrollVertically(1)) {//make your call here and pass the pagination count} – Rahul Sharma Sep 08 '22 at 07:02
  • What should I pass in pagination count can you please tell with the info I provided. It will be very helpful if you provide code so I will replace with mine code accordingly. –  Sep 08 '22 at 08:49
  • Declare a global variable "int page" in the activity, increase the variable page+=2; everytime before you make a network call. pass the page in the network call, and you will get the data, when you get the data list just add the list in the adapter's list. – Rahul Sharma Sep 08 '22 at 09:45