2

My setup consists of two tabs setup with viewpager. Each tab contains a fragment which contains a recyclerview that displays list of songs. The issue is, when loading large amount of songs into the recyclerview, the ui just freezes up for some time. For number of songs above 2000, the systems freezes for a long time and afterwards throws an OutOfMemory error.

Note: The loading of data into the recyclerview in a simple fragment or activity has no performance issues at all even when the data set is very large.

How can I optimize this?

odifek
  • 159
  • 13
  • use google's [paging](https://developer.android.com/topic/libraries/architecture/paging.html) support library – pskink Dec 08 '18 at 07:39
  • @pskink I use [MediaBrowserServiceCompat](https://developer.android.com/reference/androidx/media/MediaBrowserServiceCompat) which specifies sending list of items as List. So paging library [PagedList](https://developer.android.com/reference/android/arch/paging/PagedList) will not work here. Except I will have to get the mediaItems separately and not through MediaBrowserCompat OnSubscriptionCallback. Which I've not figured out yet. – odifek Dec 08 '18 at 20:48
  • what is that `List` in size? or how many items does it have? – pskink Dec 09 '18 at 08:17
  • The size depends on the number of songs in the phone. For a phone that has above 100 songs, the issue becomes more pronounced – odifek Dec 09 '18 at 21:31

3 Answers3

5

So I found the root course of the problem. Recyclerview inside NestedScrollView. Because I have like three RecyclerViews to display, I had to use NestedScrollView to wrap them. But then from this question, I understood that having recyclerview inside nestedScrollView will make the recyclerview want to create all the views at once before rendering them. So that was why the UI froze each time data is to be displayed.

odifek
  • 159
  • 13
0

if you just want to display basic info of songs, you can use List as data, in SongInfo contains name,album,singer...etc, it won't take much memory

Inso
  • 707
  • 5
  • 11
  • I've tried using a simple Song object with the basic song information, but still hit the same issue. Normally I used List of [MediaItem](https://developer.android.com/reference/android/support/v4/media/MediaBrowserCompat.MediaItem) which are returned from onLoadChildren of [MediaBrowserServiceCompat](https://developer.android.com/reference/androidx/media/MediaBrowserServiceCompat), but I still get the same issue when rendering the items, even after converting to simpler objects. – odifek Dec 08 '18 at 20:42
0

Easy. Just set layoutManager autoMeasureEnable = true or override LayoutmanlayoutManager.setAutoMeasureEnabled(false)

donmj
  • 191
  • 2
  • 10