Solution 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.
Solution 2 :
Try add this to your manifest.xml
android_largeHeap="true"
Solution 3 :
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.
Problem :
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:
and expanded:
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.
Comments
Comment posted by Jazib Khan
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.
Comment posted by Jazib Khan
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.
Comment posted by Anirudh
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.
Comment posted by Jazib Khan
PhonePhoto only contains the URI of the images, and all the image’s URI are already stored in ArrayList.
Comment posted by Jazib Khan
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.
Comment posted by Anirudh
Have you tried logging what layoutManager.getChildCount(); returns, that should be the number of views that are currently loaded
Comment posted by Jazib Khan
For innerRecyclerView, it is equal to the number of images.