7

My activity have some fragments in a linearLayout (R.id.liste, inside a scrollView) and it takes some time to show. I reduced the size of the array to 50 but it was first 350. Each fragment is independent since I need them to respond to click and to pop a dialog up.

Here is the code :

long start= System.currentTimeMillis();
for (Item item : dao.getList(triKey, start, end)) {
    ItemFragment fragment = ItemFragment.newInstance(item);
    FragmentTransaction fragTransaction = fragMan.beginTransaction();
    fragTransaction.add(R.id.liste, fragment);
    fragTransaction.commit(); 
}
long time = System.currentTimeMillis()-start;

Real question :

I would like to show a circle progressBar and set it Gone when all fragments are shown, but Activity doesn't have a onViewCreated() method, is there any workaround ?

Deeper, theoric question :

If I override Activity.onCreateView() and use the System.currentTimeMillis(), I see that it takes 7.5 seconds to show 50 fragments on my Nexus 4 (and 20s for 350 fragments). Since each fragment only holds 5 TextViews and 1 ProgressBar and takes like 10-15ms to create its view, I'm surprised it takes so long, why does it take nearly 6x more time to add the fragment than to draw it ?

I dont know which piece of code you would need to answer, feel free to ask of course !

Thank you very much for any help !

Dan Chaltiel
  • 7,811
  • 5
  • 47
  • 92
  • Do you really need 50-350 Fragments? What functionality, or lack thereof, is preventing you from using Views instead? – Mike M. Aug 26 '14 at 10:56
  • I would suggest you rethink your approach. Maybe one Fragment would suffice to display various data? If not, have you considered using ViewPager? – Tadej Aug 26 '14 at 10:58
  • Actually on a second thought, maybe a custom relativelayout can do the thing... but my real question was about how can I know the activity is finished drawing the UI. And of course if I could understand what takes so long to add fragments if each take 10ms to draw... I edit my first post^^ – Dan Chaltiel Aug 26 '14 at 12:45

1 Answers1

3

but Activity doesn't have a onViewCreated() method, is there any workaround ?

No.

am I doing something wrong ?

Yes.

You really need to rethink your design. You could easily do what you're describing by using a ListView either in your Activity (without using ScrollView) or use a ListView in a single ListFragment.

A ListView re-uses its list item views as it scrolls - if you use a custom Adapter it will behave as you want and is very efficient. You may also want to look at the 'view holder' pattern to making scrolling even more efficient.

Squonk
  • 48,735
  • 19
  • 103
  • 135
  • 1) OK, 2) onCreateView and onViewCreated is 2 separated things, first is when Activity show a view (even if it's inside a fragment) and second is when a view is created (which make more sense in a fragment but would be useful in an activity). 3) I understand, I'll populate my listView with custom relativelayout, would it be better ? – Dan Chaltiel Aug 26 '14 at 12:41
  • @DanChaltiel : OK, apologies for my mix-up with what you'd posted as far as `onCreateView` and `onViewCreated` - I misread your question. In reality, however, overriding `onCreateView` in the `Activity` isn't going to help you with `Fragments`. In answer to your final point in your comment - yes, I believe you'll see much performance with a `ListView` with list item layouts which use `RelativeLayout`, A `ListView` with a custom `Adapter` is very efficient even for complex list item layouts due to re-using the item views - also look up the 'view holder' pattern for `ListViews`. – Squonk Aug 26 '14 at 14:09