开发者

Android: TabHostFactory.createTabContent not being called for all tabs?

I have an app that needs tabs with one list in the content of each tab. Those lists need to refresh several times without changing the tab. The lists represent trees of possible choices, so the user clicks element 4, for example, and I have to refresh the list to show the elements under 4.

The easiest (and probably ugliest) way I found to accomplish this is to attach a listener to the list "currently" shown, and launch "this" activity again (with some extras relating the element the user clicked), effectually recreating the whole view, tabs and everything. This allows me to keep each state of the lists independent, and makes the back support easy enough. --I said it was ugly!

For that to work, I create the tabs inside onCreate, like this:

TabHost.TabSpec spec1 = tabHost.newTabSpec("TOC").setIndicator("TOC", res.getDrawable(R.drawable.ico_table_of_contents)).setContent(this);
tabHost.addTab(spec1);

TabHost.TabSpec spec2 = tabHost.newTabSpec("BLAH").setIndicator("BLAH", res.getDrawable(R.drawable.blah)).setContent(this);
tabHost.addTab(spec2);

The .setContent(this) works because this activity extends TabActivity and implements TabHost.TabContentFactory. The createTabContent function returns this:

return from(this).inflate(R.layout.new_list, getTabHost().getTabContentView(), true);

no matter which tab I'm in. After this, I update the List with an adapter, like this:

private void updateList(ArrayList<Element> items) {
  ElementAdapter adapter = new ElementAdapter(this, R.layout.element, items);
  ListView list = (ListView) findViewById(R.id.theListView);

  list.setOnItemClickListener(new AdapterView.OnItemClickListener() {...}
  list.setAdapt开发者_如何学编程er(adapter);
  adapter.notifyDataSetChanged();
}

The problem is that the first tab works great, but the second doesn't. It needs just the same view as the first one (the list showing a list of elements), but it shows nothing-zippo-nada, just the tabs. The ArrayList I pass in has elements in both cases.

Walking the code, I noticed that in my createTabs, the createTabContent is called ONCE, on tabHost.addTab(spec1) (for the first tab), but NOT when adding the second.

What gives? Why do I get an empty screen instead of the list, if both cases have elements to show?

I hope this makes sense. It's a bizarre problem to explain and I'm not sure if I'm making much sense.

Any help will be appreciated.

Thanks

llappall


I have never tried using the same TabContentFactory more than once. I would have expected what you used here to work, particularly given the tag parameter to createTabContent().

That being said, try using multiple TabContentFactory objects and see if that helps (e.g., use instances of anonymous inner classes).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜