开发者

Alternatives to using an activity for each tab

The scenerio is like this. Currently I am using the following code

TabSpec setContent = tabhost.newTabSpe开发者_JAVA技巧c("tab")
                .setIndicator("tabview")
                .setContent(new Intent(tabhost.getContext(), someActivity.class));

But I am told that each tab should not be associated with an activity and we must follow code something like this.

TabSpec setContent = tabhost.newTabSpec("tab").setIndicator("tabView").setContent(R.id.layout)

Consider a scenario where tab1 calls camera app, tab2 parses an XML and tab3 does some other display work. How do I solve this ? Because as soon as tab is changed I must call these methods. How do I create a single activity and assign all responsibilities to it ?


You can create a single activity with tabs that show only views. The only catch is that the views have to be defined inside the tag.

<FrameLayout
        android:id="@android:id/tabcontent"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent">
      <ListView 
        android:id="@+id/list1" 
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" 
        android:layout_weight="1"/>
      <ListView
          android:id="@+id/list2"
          android:layout_width="fill_parent"
          android:layout_height="fill_parent"
          android:layout_weight="1" /> 
</FrameLayout>

Then, when inside your onCreate in your TabActivity:

TabHost tabs = getTabHost();
TabHost.TabSpec commentsTab = tabs.newTabSpec(TAB_TAG_1);
tabs.addTab(commentsTab.setContent(R.id.list1));

TabHost.TabSpec infoTab = tabs.newTabSpec(TAB_TAG_2);
tabs.addTab(infoTab.setContent(R.id.list2));

Note that I did not specify indicators for either tab, in the interests of space.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜