Android - Tabs, MapView, activities within tabs

后端 未结 3 927
被撕碎了的回忆
被撕碎了的回忆 2020-12-08 03:45

We\'re in the process of writing an app that has 4 tabs: Map, People, Places, Events. The People, Places, and Events in the App show up as Icons on the map. By default the

3条回答
  •  旧时难觅i
    2020-12-08 04:12

    It's a good practice to have one activity and multiple views for your tabs. However, that does mean you have to be careful to handle which tab is selected, different menus and context menus for each tab view, etc.

    I guess my question is, is there a GOOD tutorial somewhere showing exactly how to do complex tasks with a TabHost? I've seen HelloTabWidget; I'm looking for something much more sophisticated than this.

    I wrote a slightly better tutorial on my blog that demonstrates an interacting ListView and MapView as tabs. Here's the link: Android Tabs with interacting map and list views

    The basics is to have a layout similar to the one in the HelloTabWidget tutorial, make your activity extend from MapActivity, extract the tabhost from the XML and make sure you call setup() on the tabhost. After that, adding views as the content of tabs, tab listeners, etc. are the same.

    Here's a brief starting point for the class:

    public class TabbedListMapActivity extends MapActivity {
    
    private ListView listView;
    private MapView mapView;
    
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    
        tabHost = (TabHost) findViewById(android.R.id.tabhost);
    
        // setup must be called if not a TabActivity
        tabHost.setup();
    
        // setup list view
        listView = (ListView) findViewById(R.id.list);
    
        // setup map view
        mapView = (MapView) findViewById(R.id.mapview);
    
        // add views to tab host
        tabHost.addTab(tabHost.newTabSpec("List").setIndicator("List").setContent(new TabContentFactory() {
            public View createTabContent(String arg0) {
                return listView;
            }
        }));
        tabHost.addTab(tabHost.newTabSpec("Map").setIndicator("Map").setContent(new TabContentFactory() {
            public View createTabContent(String arg0) {
                return mapView;
            }
        }));
    }
    

    ...

提交回复
热议问题