2層TabHostネスト

7228 ワード

メインタブホスト
public class MainActivity extends ActivityGroup{


	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		TabHost tabHost = (TabHost) findViewById(R.id.tabhost);
		tabHost.setup(this.getLocalActivityManager());

		tabHost.addTab(tabHost.newTabSpec("tab1").setIndicator("First Tab")
				.setContent(new Intent(this, MainActivity1.class)));

		tabHost.addTab(tabHost.newTabSpec("tab2").setIndicator("Second Tab")
				.setContent(new Intent(this, MainActivity1.class)));

		tabHost.addTab(tabHost.newTabSpec("tab3").setIndicator("Third Tab")
				.setContent(new Intent(this, MainActivity1.class)));
		tabHost.setCurrentTab(0);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

}

プライマリTabHostレイアウトファイル
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >
    <TabHost
        android:id="@+id/tabhost"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        >
        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            >
              <TabWidget
                android:id="@android:id/tabs"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_alignParentBottom="true"
                >
            </TabWidget>
            <FrameLayout
                android:id="@android:id/tabcontent"
                android:layout_width="match_parent"
                android:layout_height="match_parent" >
            </FrameLayout>
          
        </RelativeLayout>
    </TabHost>
 
</RelativeLayout>

子TabHost
public class MainActivity1 extends Activity {

	private ListView listView;
	private List<String> currentData;//  
	private CustomAdapter customadapter;//  
	private View loadingView;//  

	private int currentPage = 1;//  , 1
	private int pageSize = 20;//  
	private int last_item_position;//  item 

	private boolean isLoading = false;//  , 

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main1);

		TabHost tabHost = (TabHost) findViewById(R.id.tabhost);
		tabHost.setup();
		TabWidget tabWidget = tabHost.getTabWidget();
		tabHost.addTab(tabHost.newTabSpec("tab1").setIndicator("First Tab")
				.setContent(R.id.tab1));

		tabHost.addTab(tabHost.newTabSpec("tab2").setIndicator("Second Tab")
				.setContent(R.id.tab2));

		tabHost.addTab(tabHost.newTabSpec("tab3").setIndicator("Third Tab")
				.setContent(R.id.tab3));
		tabHost.setCurrentTab(0);
		int height = 30;
		// int width =45;
		for (int i = 0; i < tabHost.getChildCount(); i++) {

			/**  、 , fill_parent,  */
			tabWidget.getChildAt(i).getLayoutParams().height = height;
			// tabWidget.getChildAt(i).getLayoutParams().width = width;
		}
		tabHost.setOnTabChangedListener(new MyTabListener(this));
	}

レイアウトファイル
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >
 
    <TabHost
        android:id="@+id/tabhost"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true" >
 
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical" >
 
            <TabWidget
                android:id="@android:id/tabs"
                android:layout_width="match_parent"
                android:layout_height="wrap_content" >
            </TabWidget>
 
            <FrameLayout
                android:id="@android:id/tabcontent"
                android:layout_width="match_parent"
                android:layout_height="match_parent" >
 
             <LinearLayout
                    android:id="@+id/tab1"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent" >
 				<ListView android:id="@+id/lv_id"
				 android:layout_width="fill_parent"
				 android:layout_height="wrap_content" />         
                </LinearLayout>
 
                <LinearLayout
                    android:id="@+id/tab2"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent" >
                    <Button
                        android:id="@+id/button1"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="Second Tab Content" />
                </LinearLayout>
 
                <LinearLayout
                    android:id="@+id/tab3"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent" >
                    <Button
                        android:id="@+id/button1"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="Third Tab Content" />
                </LinearLayout>
            </FrameLayout>
        </LinearLayout>
    </TabHost>
 
</RelativeLayout>