2011-01-10 161 views
4

現在我在我的應用程序tabhost中使用單個活動和單獨的視圖。在TabHost中更改視圖(一個活動,多個視圖)

選項卡活動:

super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 

    TabHost tabs = (TabHost) findViewById(R.id.tabhost);   
    tabs.setup(); 

    TabHost.TabSpec spec = tabs.newTabSpec(TAB_HOTELS); 
    spec.setContent(R.id.tab1); 
    spec.setIndicator("Hotels"); 
    tabs.addTab(spec); 

    spec = tabs.newTabSpec(TAB_LAST_MINUTE); 
    spec.setContent(R.id.tab2); 
    spec.setIndicator("Last Minute"); 
    tabs.addTab(spec); 

佈局

<?xml version="1.0" encoding="utf-8"?> 
<TabHost xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+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"> 
     <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"> 
      <LinearLayout android:layout_width="fill_parent" 
       android:layout_height="fill_parent" android:id="@+id/tab1" 
       android:orientation="vertical"> 
       <ListView android:id="@+id/listaLocalita" 
        android:layout_width="fill_parent" android:layout_height="wrap_content" /> 
      </LinearLayout> 

      <LinearLayout android:layout_width="fill_parent" 
       android:layout_height="fill_parent" android:id="@+id/tab2" 
       android:orientation="vertical" android:paddingTop="60px"> 
       <TextView android:layout_width="fill_parent" 
        android:layout_height="100px" android:text="This is tab 2" 
        android:id="@+id/txt2" /> 
      </LinearLayout> 
     </FrameLayout> 
    </LinearLayout> 
</TabHost> 

,事情是我使用的一個一級深度相當不錯,只要工作。不幸的是,現在我需要使其中一個選項卡工作如下:

Tab加載ListView與城市列表,用戶點擊城市名稱,選項卡的內容被替換爲酒店列表,用戶選擇一家酒店,總是在同一個標​​籤頁中,酒店的信息被加載。

我該如何實現這種情況? LayoutInflater?

在此先感謝。

+0

你有沒有想過這個?我正在尋找類似的東西。 – 2011-11-16 20:53:02

+0

只看[本例](http://ericharlow.blogspot.in/2010/09/experience-multiple-android-activities.html)。希望它會幫助你。 – 2012-02-04 04:30:32

回答

0

您可以使用intent將Activity設置爲標籤內容,並且此活動可以調用其他活動(listView或任何其他活動)。

/** tid1 is firstTabSpec Id. Its used to access outside. */ 
     TabSpec MainTab = tabHost.newTabSpec("tag1"); 
     TabSpec SearchTab = tabHost.newTabSpec("tag2"); 
     TabSpec MessageTab = tabHost.newTabSpec("tag3"); 
     TabSpec ServicesTab = tabHost.newTabSpec("tag4"); 
     TabSpec SettingsTab = tabHost.newTabSpec("tag5"); 

     MainTab.setIndicator("Main", res.getDrawable(R.drawable.anchor_36)); 
     MainTab.setContent(new Intent(this, MainView.class)); 

    SearchTab.setIndicator("Search", res.getDrawable(R.drawable.clock_36)); 
    SearchTab.setContent(new Intent(this, SearchView.class)); 

    MessageTab.setIndicator("Messages", res 
      .getDrawable(R.drawable.dialog_36)); 
    MessageTab.setContent(new Intent(this, MessagesView.class)); 

    ServicesTab.setIndicator("Services", res 
      .getDrawable(R.drawable.cloud_36)); 
    ServicesTab.setContent(new Intent(this, ServicesView.class)); 

    SettingsTab.setIndicator("Settings", res 
      .getDrawable(R.drawable.settings_36)); 
    SettingsTab.setContent(new Intent(this, SettingsView.class)); 

    /** Add tabSpec to the TabHost to display. */ 
    tabHost.addTab(MainTab); 
    tabHost.addTab(SearchTab); 
    tabHost.addTab(MessageTab); 
    tabHost.addTab(ServicesTab); 
    tabHost.addTab(SettingsTab); 

現在,在加載活動,你可以使用如下代碼

StartActivity(new intent(currentActivity.this, newActivity.class)); 

這會爲你工作。