2011-04-10 79 views
4

我有兩個選項卡的TabActivity。活動的佈置如下:TabHost在開頭沒有顯示內容

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

    <TabWidget android:id="@android:id/tabs" 
     android:layout_width="fill_parent" android:layout_height="wrap_content" /> 
    <FrameLayout android:id="@android:id/tabcontent" 
     android:layout_width="fill_parent" android:layout_height="fill_parent" 
     android:padding="5dp"> 
     <ListView android:id="@+id/list" 
      android:layout_width="fill_parent" android:layout_height="fill_parent" /> 

    </FrameLayout> 
</LinearLayout> 
</TabHost> 

佈局由(),其在相應setOnTabChangedListener填充一個ListView的。填寫清單並顯示它沒有問題。

我的問題是,活動開始時不顯示列表視圖,雖然我通過ID找到它並填充它;它只是在我更改標籤後填充。

,並在OnCreate(..)的代碼如下所示:

l = (ListView) findViewById(R.id.list); 
l.setAdapter(new CommentsAdapter(this, (JSONArray) obj)); 

TabSpec spec; 
    spec = tabHost.newTabSpec("0").setIndicator("0", 
      res.getDrawable(R.drawable.tab_recent)).setContent(
      R.id.list); 
    tabHost.addTab(spec); 

    spec = tabHost.newTabSpec("1").setIndicator("1", 
      res.getDrawable(R.drawable.tab_pop)).setContent(
      R.id.list); 
    tabHost.addTab(spec); 

    tabHost.setOnTabChangedListener(new OnTabChangeListener() { 

     @Override 
     public void onTabChanged(String tabId) { 
      // TODO Auto-generated method stub 
      int tab = Integer.valueOf(tabId); 
      showDialog(WAIT_DIALOG); 

      switch (tab) { 
      // recent 
      case 0: 
       //refill the list 
       break; 
      // popular 

      case 1: 
       //refill the list 
       break; 
      } 
     } 
    }); 

    tabHost.setCurrentTab(0); 

任何提示?

+0

就可以把標籤的所有代碼,使我們可以有外觀和告訴 – Sumant 2011-04-10 12:58:59

+0

只是把Java代碼及新界XML堂妹烏爾XML可能被罰款ontab更改列表視圖是顯示 – Sumant 2011-04-10 13:13:04

回答

-2

你只是做其他活動中列表視圖中存在&把下面的一個

Intent i1 = new Intent(tabs.this,listDisplay.class); 
spec=tabHost.newTabSpec("0").setIndicator("0",res.getDrawable(R.drawable.      tab_recent)).setContent(i1);           

也把tabHost.setCurrentTab(0);ontabChangeListener希望這可以幫助..

希望這可能解決問題..

+1

我不想使用單獨的活動@Sumant。該解決方案是無效的。 – olix20 2011-04-10 13:32:41

8

我有一個類似的問題。我注意到,如果你這樣做

tabHost.setCurrentTab(0); 

然後onTabChanged沒有觸發。但是,如果你這樣做

tabHost.setCurrentTab(1); 

然後它是,列表顯示出來,它都很好。至於爲什麼會發生這種情況,對我來說這仍然是一種莫名其妙的感覺。

使用此作弊在onCreate()的第一個選項卡上啓動您的應用程序: ,先創建選項卡調用後: tabHost.setCurrentTab(1); 然後調用: tabHost.setCurrentTab(0);

+0

這可能工作,如果我的第二個標籤沒有嘗試更新其內容之前,第一個執行,'java.lang.IllegalStateException:內容視圖尚未創建' – 2014-03-17 03:09:09

+0

@MikeFlynn沒有更多的上下文我不知道如何回覆 – Stephan 2014-03-17 17:35:08

+1

我面臨這個問題,即使5年後有沒有人得到解決方案? – Imdad 2016-07-22 05:59:17

1

您可以嘗試使用不同的佈局,說

spec = tabHost.newTabSpec("0").setIndicator("0", 
     res.getDrawable(R.drawable.tab_recent)).setContent(
     R.id.list1); 
tabHost.addTab(spec); 

spec = tabHost.newTabSpec("1").setIndicator("1", 
     res.getDrawable(R.drawable.tab_pop)).setContent(
     R.id.list2); 
tabHost.addTab(spec);