1

I have three LinearLayout views where I load some of my own views like that:

linearLayout1.addView(view11);
linearLayout1.addView(view12);
linearLayout2.addView(view21);
linearLayout2.addView(view22);
linearLayout3.addView(view31);
linearLayout3.addView(view32);

The problem is that when I have to load many views (more than 30) the performance drops. Specifically, this happens:

I start the activity, the activity is rendered, I load from the local sqlite the required data to load on screen and I start to add my custom views to the linear layouts.

At that point, the app freeze and after 2-3 seconds, all my custom views appear all together.

I tried putting a Log inside each custom view's onMeasure and I found out that each view is indeed ready almost at the same time (<1ms difference between them).

I am looking for a way to know when everything is ready and the user is able to interact with the UI again.

Specifically, I want to show a ProgressDialog when the activity starts and hide it after those 2-3 seconds, when the screen is ready for the user.

Any ideas?

Also, would it help the overall performance if I replace my LinearLayouts with ListViews and add Custom Adapters to add my custom views instead of using addView?

TheCrafter
  • 1,909
  • 2
  • 23
  • 44
  • Yes! Do not use LinearLayout even ScrollView to render multiple view. Just use a single listView. – teck wei Dec 25 '16 at 18:49
  • @teckwei would that really have so great impact to performance? even for much more views (lets say 200) ? – TheCrafter Dec 25 '16 at 18:50
  • 2
    Did you saw twitter feed, facebook feed? They all using listView even you having 1000 view they can render it well just because they using the reuse technique. It mean your view not actually inside the memory or not even exists when they are not visible. They will create or reuse once u scroll to the possible. But you have do much more work if you do have many difference layout. – teck wei Dec 25 '16 at 18:54
  • @teckwei Okay thanks. I will refactor my activity to use ListView. – TheCrafter Dec 25 '16 at 18:57
  • @TheCrafter Use RecyclerView. ListView is old and clunky. – Karakuri Dec 25 '16 at 19:14
  • @Karakuri You got it. thanks :) – TheCrafter Dec 25 '16 at 22:37

1 Answers1

0

Yes for sure you have to use RecyclerView or ListView with Holder pattern. They use same view(reuse) for displaying content.

New view creation is a costly work and for sure it will affect performance. If you have different type of views then you can define multiple views fro your RecyclerView.

This might help you.

Adding Views will also invalidate its parent view, so every time you add new view inside your ViewGroup then ViewGroup have to redraw.

Community
  • 1
  • 1
Rahul
  • 10,457
  • 4
  • 35
  • 55