2011-01-10 7 views
4

今私は、1つのアクティビティと別々のビューでアプリケーションのタブホストで使用しています。TabHost内のビューを変更する(1つのアクティビティ、複数のビュー)

タブアクティビティ:

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> 

物事は限り私は1つのレベルの深さを使用していると非常によく働いています。残念ながら、次のようにタブの1つを作成する必要があります。

タブのリストListViewで都市名のリストが表示され、ユーザーが都市名をクリックするとタブのコンテンツがホテルのリストに置き換えられ、ユーザーがホテルを選択し、常に同じタブにあり、ホテルの情報が読み込まれます。

このシナリオをどのように達成できますか? LayoutInflater?

ありがとうございます。

+0

あなたはこれを理解しましたか?私は似たようなことをしています。 –

+0

[この例](http://ericharlow.blogspot.in/2010/09/experience-multiple-android-activities.html)を参照してください。それがあなたを助けることを願っています。 –

答えて

0

あなたは、アクティビティをタブコンテンツとして設定する意図を使用することができ、このアクティビティは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)); 

このようなコードを使用することができます。

+0

shureしかし、これは 'newActivity'をTab内ではなく一つのWindowで起動します... –

関連する問題