开发者

How to approach Android tabs and subtabs

I am looking to c开发者_运维知识库reate an Android application that uses Tabs.

Requirements

When a user selects a tab and gets back to the same tab, there shouldn't be a loading period (preferably).

Each tab will contain a listview.

In one of the tabs, I will have to use subtabs.

And that's it.

One great example I really liked (although I didn't try the app) is http://www.usatoday.com/android/.


Two thinks you need to consider:

1.) Subtabs cannot be implemented using Activities, since an ActivityGroup cannot wrap another ActivityGroup

2.) Tabs in Tabs blow up Layout complexity quite a bit. Especially on < 2.0 devices you'll easily run into StackOverflowExceptions. There was also a blog post about it on the official android blog, I'll update the answer when I find it.

[EDIT] There we go: http://android-developers.blogspot.com/2009/04/future-proofing-your-apps.html

The relevant part is:

Due to changes in the View rendering infrastructure, unreasonably deep (more than 10 or so) or broad (more than 30 total) View hierarchies in layouts are now likely to cause crashes. This was always a risk for excessively complex layouts, but you can think of Android 1.5 as being better than 1.1 at exposing this problem.

Considering that every tabhost adds about 4 layers of depth, you'll reach a depth of 10 with double tabs pretty soon.

Keep in mind that "using activities" as tabs is just a sham, they are just additional views within your hierarchy.


I just developed a tabbed application and I used the following function to create tabs in code. The function uses intents which mean when ever a tab is selected an Activity is loaded in the tab body using intent. This function is called in main activity.

private void SetupTabs()
    {
        TabHost tabHost = getTabHost();


        TabHost.TabSpec spec;  
        Intent intent;  

        //*****************************
        intent = new Intent().setClass(MPaper.this, NewsList.class);        
        spec = tabHost.newTabSpec(NewsTag).setIndicator("News")
                      .setContent(intent);        
        tabHost.addTab(spec);
        //*****************************
        intent = new Intent().setClass(MPaper.this, Search.class);        
        spec = tabHost.newTabSpec(SearchTag).setIndicator("Search")
                      .setContent(intent);        
        tabHost.addTab(spec);
        //****************************
        intent = new Intent().setClass(MPaper.this, MyNewsList.class);        
        spec = tabHost.newTabSpec(MyNewsTag).setIndicator("My News")
                      .setContent(intent);        
        tabHost.addTab(spec);
        //*****************************
        intent = new Intent().setClass(MPaper.this, Extras.class);        
        spec = tabHost.newTabSpec(ExtrasTag).setIndicator("Extras")
                      .setContent(intent);        
        tabHost.addTab(spec);
        //*****************************


        tabHost.setCurrentTabByTag(NewsTag);
    }

The list view in NewsList Activity is made by XML as following:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/tabhost" 
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">

    <ListView 
        android:id="@+id/NewsList" 
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content"
        >

    </ListView>

</LinearLayout>

In Response to Fabios' comment below: Yes wither use visibility property or try implementing tabs using the way mentioned below:

private void TabSetup()
    {
        tabs = (TabHost)findViewById(R.id.tabhost);        
        tabs.setup(); 
        TabHost.TabSpec spec = tabs.newTabSpec("MainNewsTag");
        spec.setContent(R.id.MainNews);
        spec.setIndicator("Home");
        tabs.addTab(spec);




        spec = tabs.newTabSpec("SearchTag");
        spec.setContent(R.id.Search);
        spec.setIndicator("Search");
        tabs.addTab(spec);


        spec = tabs.newTabSpec("MyNewsTag");
        spec.setContent(R.id.MyNews);
        spec.setIndicator("My News");
        tabs.addTab(spec);

        spec = tabs.newTabSpec("ExtrasTag");
        spec.setContent(R.id.MyNews);
        spec.setIndicator("Extras");
        tabs.addTab(spec);
} 

Where as Layout file is as below:

    <TabHost xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/tabhost" android:layout_width="fill_parent"
        android:layout_height="fill_parent">
        <RelativeLayout android:layout_height="fill_parent"
            android:layout_width="fill_parent">
            <TableLayout android:layout_height="fill_parent" 
               android:layout_width="fill_parent">
               <TabWidget android:id="@android:id/tabs"
               android:layout_height="wrap_content"
                   android:layout_width="fill_parent" />
               <FrameLayout android:id="@android:id/tabcontent"
                    android:layout_height="wrap_content"
                                android:layout_width="fill_parent" >  
                   <ListView android:id="@+id/MainNews" 
                             android:layout_width="fill_parent"
                             android:layout_height="wrap_content">
                   </ListView>
.... and so on 
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜