0

first question on SO, forgive me if I forget to include something.

In my activity, I have a function that loads a new activity that has a tabbed ViewPager with two fragments. From what I understand (or rather, from what I can tell) the activity does not load until both tabs have completed their onCreateViews() functions. Currently, this is taking between 500ms-2000ms which makes the application feel rather clunky. I believe this gives me two options:

  1. Have the activity load and display the first tab once the first tab has finished it's onCreateView()
  2. Preferably, speed up the loading of the second tab/fragment. What I'm doing is very slow and I'm looking for a better way to do it.

Here is an excerpt from my code for the second fragment (tab):

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

    View root_view = inflater.inflate(R.layout.stats_layout, container, false);

    for (Map.Entry<String, int[]> entry : stats.entrySet()) {
        String key = entry.getKey();
        int[] value = entry.getValue();
        if (value[2] == 1) {
            LinearLayout ll_singles = (LinearLayout) root_view.findViewById(R.id.ll_singles_stats);
            RelativeLayout rl_detail = (RelativeLayout) inflater.inflate(R.layout.rl_detail, ll_singles, false);
            ( (TextView) rl_detail.findViewById(R.id.tv_made) ).setText(Integer.toString(value[0]));
            ( (TextView) rl_detail.findViewById(R.id.tv_total) ).setText(Integer.toString(value[0] + value[1]));
            ( (TextView) rl_detail.findViewById(R.id.tv_percent) ).setText(MessageFormat.format("{0,number,0.00%}", (float) value[0] / (value[0] + value[1])));
            ll_singles.addView(rl_detail);
     }
}

Without getting too specific on my entire layout (unless someone asks), I inflate a single RelativeLayout (rl_detail) many (50+) times and add it to four different LinearLayouts (in this excerpt, ll_singles).

I know findViewById() is an expensive action and I have to think there is a way to avoid the findViewById() that I do every time I inflate the exact same view when I change the text for the TextView.

I have explored asynctasks (haven't been able to find an example that inflates views in the background -- it sounds like it isn't possible) and have considered using a ViewHolder, but that seems exclusive to ListViews.

Any ideas?

  • 2
    Sounds like you should be using a ListView instead of a LinearLayout to hold those 50+ views. A ListView only inflates as many views as will fit on screen, and then it reuses off-screen ones when new ones scroll onto the screen, which cuts way down on wasted time instantiating and laying out views. There will be some learning curve to do a custom layout for each list item like you probably want. You need to look up how to do custom ListAdapters. – Tenfour04 Mar 06 '14 at 17:40
  • @Tenfour04 is correct. Use a ListView. – Krylez Mar 06 '14 at 17:56
  • While the biggest case I have I need to inflate 50+ views, there are also common cases when it's only going to be ~0-5 views. Would a ListView still be applicable? – aplewandowski Mar 06 '14 at 18:12

0 Answers0