Android : Activity onviewcreated

Dan Chaltiel picture Dan Chaltiel · Aug 26, 2014 · Viewed 12.6k times · Source

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 !

Answer

Squonk picture Squonk · Aug 26, 2014

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.