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:
- Have the activity load and display the first tab once the first tab has finished it's onCreateView()
- 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?