Mulgard
Mulgard

Reputation: 10569

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?

Upvotes: 1

Views: 1604

Answers (2)

Avanish Singh
Avanish Singh

Reputation: 288

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

Upvotes: 3

Nayan Srivastava
Nayan Srivastava

Reputation: 3725

Sometimes when you really have great amount of data and have to some heavy processing over them, lazy loading of object is a better approach. Since 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. This would avoid the frustration of UI freezing up.

Other approach could be to load your data in pieces and make use of methods like notifyItemRangeInserted() which are more efficient than simple notifyDataSetChanged().

Upvotes: 1

Related Questions