Alternatives to using an activity for each tab

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-12 01:29:09

问题


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

TabSpec setContent = tabhost.newTabSpec("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 ?


回答1:


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.



来源:https://stackoverflow.com/questions/5694406/alternatives-to-using-an-activity-for-each-tab

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!