0

I wanted to create a recycler view of photo albums and when the user taps on an album, it expands to grid layout showing images in that album.

For example, non-expanded:

enter image description here

and expanded:

enter image description here

The layout of outer recyclerView item only contains a textView, checkbox, and an invisible recyclerview that becomes visible on click of the item.

I declared the outerAdapter like this:

photosVideosAdapter = new PhotosVideosAdapter(getContext(),new ArrayList<PhoneAlbum>());
recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
recyclerView.setAdapter(photosVideosAdapter);

Here is the onBindViewHolder of outer adapter:

holder.tvFolder.setText(phoneAlbums.get(position).getName());
ArrayList<PhonePhoto> phonePhotos = phoneAlbums.get(position).getAlbumPhotos();
InnerAdapter innerAdapter = new InnerAdapter(context, phonePhotos);
holder.innerRecyclerView.setHasFixedSize(true);
holder.innerRecyclerView.setLayoutManager(new GridLayoutManager(context,3));
holder.innerRecyclerView.setAdapter(innerAdapter);
.... OnClickMethod to toggle visibility of innerRecyclerView

And onBindViewHolder for inner adapter:

Glide.with(context)
        .load(phonePhotos.get(position).getPhotoUri())
        .override(200,200)
        .into(holder.imgThumbnail);

The problem is that if inner recyclerView contains more than 200 items, the app crashes due to high memory usage. But as you can see, I'm using glide to load images and also RecyclerView shouldn't create all the views at once. But, what I can see is that the inner RecyclerView is creating all the item views at once which is causing the app to crash.

How can I fix this problem? Any help will be appreciated.

Jazib Khan
  • 408
  • 4
  • 13
  • The problem is that the inner recyclerView is loading all the views at once. So, if the inner recyclerView has 1000 images, all images are loaded at once, which is not what recyclerView is supposed to do. – Jazib Khan Jul 12 '20 at 16:46

3 Answers3

1

Trust me, dont go with recyclerview inside recyclerview. I had the similar situation in my current app where my colleague built solution by using nested recyclerview.

I rewrote the entire logic using insert and delete with animation and multiple view type. It will involve some extra code to manage it. But the result would be quite satisfying.

In fact, I used same logic in iOS collectionview as well. App on both platform is live.

Sandeep Dhull
  • 1,638
  • 2
  • 18
  • 30
0

Try add this to your manifest.xml

        android:largeHeap="true"

  • The problem is not about increasing the heap size. The problem is that the inner recycler view is creating all the view at once, so, if there are 4000 photos in an album, it will create all the 4000 view at once. – Jazib Khan Jul 12 '20 at 16:41
0

Your memory consumption is likely to high because you retrieved all the photos from the album at once with the line:

ArrayList<PhonePhoto> phonePhotos = phoneAlbums.get(position).getAlbumPhotos();

I assume you are also pulling all the photos at their original quality, so having 200+ full size photos on phone's RAM all at once could be the cause of the crash.

One way you could fix this would be to load in lower resolution previews of the photos into RAM, and once a photo is actually being displayed in the RecyclerView you could load a full resolution photo.

Anirudh
  • 2,648
  • 2
  • 13
  • 16
  • One way you could test if this is actually the root cause of your problem would be to comment out all the code regarding the displaying the pictures in a RecylcerView and seeing if the memory usage is still very high. – Anirudh Jul 12 '20 at 16:44
  • PhonePhoto only contains the URI of the images, and all the image's URI are already stored in ArrayList. – Jazib Khan Jul 12 '20 at 16:44
  • The problem is that the inner recycler view is loading all the views at once. So, if inner recycler view has 1000 images, all images are loaded at once, which is not what recyclerView is supposed to do. – Jazib Khan Jul 12 '20 at 16:44
  • Have you tried logging what layoutManager.getChildCount(); returns, that should be the number of views that are currently loaded – Anirudh Jul 12 '20 at 16:58
  • For innerRecyclerView, it is equal to the number of images. – Jazib Khan Jul 12 '20 at 17:02
  • Other answer say it could have something to do with your layout – Anirudh Jul 12 '20 at 17:12