Jazib Khan
Jazib Khan

Reputation: 440

RecyclerView inside RecyclerView, app crashing due to high memory consumption

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.

Upvotes: 0

Views: 1431

Answers (3)

Anirudh
Anirudh

Reputation: 2796

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.

Upvotes: 0

Sandeep Dhull
Sandeep Dhull

Reputation: 1648

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.

Upvotes: 1

quadri adedayo
quadri adedayo

Reputation: 41

Try add this to your manifest.xml

        android:largeHeap="true"

Upvotes: 0

Related Questions