Search code examples
androidperformanceandroid-viewpagerandroid-recyclerview

Optimize ViewPager with RecyclerViews containing heavy data


I have a ViewPager containing five Fragment's. Each Fragment contains much data. For example the first Fragment is a list of 600 entries containing text and images. The other four Fragment's are containing diagrams which are creating evaluations from the list of the 600 values. The optimizations I implemented yet are:

  1. No Database selects inside the adapter
  2. No Image Loading inside the adapter (Im using Picasso)
  3. No data parsing inside the adapter

I also load the entries for the Adapter inside an AsyncTask. For the RecyclerView's i set the following options:

this.listRowParent.setItemViewCacheSize(5);
this.listRowParent.setDrawingCacheEnabled(true);
this.listRowParent.setDrawingCacheQuality(View.DRAWING_CACHE_QUALITY_HIGH);
this.listRowParent.setHasFixedSize(true);

The scrolling is very smooth with all my optimizations. The problem that i have is what happens when the fragment becomes visible. The moment when i set the adapter the ui starts freezing (skipping between 100 and 300 frames). I tried to solve it increasing the OffscreenPageLimit:

this.viewPager.setOffscreenPageLimit(5);

but that just helps sometimes. I guess the interal initialization of the adapter when it is set is performing too much work on the ui thread but i have no idea how i could improve the performance. The only operations i perform inside the adapter and the onBindViewHolder methods are comparisons in list initializations.

How can i further improve the performance? Would it be possible for example to place the code inside the onBincViewHolder inside an AsyncTask or would that not be possible due to the mechanisms of a RecyclerView?


Solution

  • Yes it is possible to place the code inside the onBincViewHolder inside an AsyncTask. you have not provided much code, I can only suggest to separate your UI logic and data processing logic and then perform data processing logic into a worker thread and meanwhile show a progress dialog to user to notify that something is happening in background