android tabhost选项卡和视图不匹配

时间:2011-12-12 09:33:09

标签: android tabs

我的tabwidget中有两个标签,每个标签显示一个列表视图。 AY 最初我的第一个标签显示为已选中,但下方列表对应第二个标签。 一旦我点击标签,我就会得到正确的显示。

private static final String LIST_TAB_TAG1 = "UpcomingEvents";
private static final String LIST_TAB_TAG2 = "PastEvents";

tabHost.addTab(tabHost.newTabSpec(LIST_TAB_TAG1)
            .setIndicator(LIST_TAB_TAG1)
            .setContent(new TabContentFactory() {
                public View createTabContent(String arg) {
                    return listView1;
                }
            }));
    tabHost.addTab(tabHost.newTabSpec(LIST_TAB_TAG2)
            .setIndicator(LIST_TAB_TAG2)
            .setContent(new TabContentFactory() {
                public View createTabContent(String arg) {
                    return listView2;
                }
            }));
    tabHost.setCurrentTab(0);

LIST_TAB_TAG1在启动此sctivity时突出显示,但显示的列表为listview2。此问题仅在活动开始时出现。单击选项卡后,其工作正常

请帮我解决这个问题。谢谢你的时间

1 个答案:

答案 0 :(得分:0)

我通常使用docs中给出的代码,对我来说很好,试试这个。

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    TabHost tabHost = getTabHost();  // The activity TabHost
    TabHost.TabSpec spec;  // Resusable TabSpec for each tab

    Intent intent;  // Reusable Intent for each tab
    // Create an Intent to launch an Activity for the tab (to be reused)
    intent = new Intent().setClass(this, UpcomingEvents.class);
    // Initialize a TabSpec for each tab and add it to the TabHost
    spec = tabHost.newTabSpec("UpcomingEvents").setIndicator("UpcomingEvents",getResources().getDrawable(R.drawable.homebutton1)).setContent(intent);
    tabHost.addTab(spec);
    // Do the same for the other tabs
    intent = new Intent().setClass(this,PastEvents.class);
    spec = tabHost.newTabSpec("PastEvents").setIndicator("PastEvents",
                      getResources().getDrawable(R.drawable.bank_transaction1))
                  .setContent(intent);
    tabHost.addTab(spec);

    tabHost.setCurrentTab(0);
  }