开发者

Dynamic tab in Android with dynamic tab content

I want to create a tab dynamically in Android and under each tab I have one listview. So I want the content of the listview to also change dynamica开发者_开发百科lly. How can I do this ?


I saw a code on another forum and cleaned it up a little. To do what you need you'll have to change the stuff inside of ts1.setContent and ts2.setContent

Main_screen.java

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TabHost;
import android.widget.TabHost.TabSpec;

public class Main_screen extends Activity{

    private ListView ls1;
    private ListView ls2;   
    private TabHost myTabHost;

    @Override
    public void onCreate(Bundle icicle)
    {
        super.onCreate(icicle);
        setContentView(R.layout.main);

        myTabHost = (TabHost)this.findViewById(R.id.th_set_menu_tabhost); 
        myTabHost.setup();

        ls1 = new ListView(Main_screen.this);       
        TabSpec ts1 = myTabHost.newTabSpec("TAB_TAG_1");
        ts1.setIndicator("Tab1");
        ts1.setContent(new TabHost.TabContentFactory(){
            public View createTabContent(String tag)
            {                                   
                ArrayAdapter<String> adapter = new ArrayAdapter<String>(Main_screen.this,android.R.layout.simple_list_item_1,new String[]{"item1","item2","item3"});
                ls1.setAdapter(adapter);    
                return ls1;
            }       
        });
        myTabHost.addTab(ts1);

        ls2 = new ListView(Main_screen.this);
        TabSpec ts2 = myTabHost.newTabSpec("TAB_TAG_2");
        ts2.setIndicator("Tab2");
        ts2.setContent(new TabHost.TabContentFactory(){
            public View createTabContent(String tag)
            {                                   
                ArrayAdapter<String> adapter = new ArrayAdapter<String>(Main_screen.this,android.R.layout.simple_list_item_1,new String[]{"item4","item5","item6"});
                ls2.setAdapter(adapter);    
                return ls2;
            }       
        });     
        myTabHost.addTab(ts2);
    }       
}

main.xml

<?xml version="1.0" encoding="utf-8"?> 
    <TabHost xmlns:android="http://schemas.android.com/apk/res/android" 
        android:id="@+id/th_set_menu_tabhost" 
        android:layout_width="fill_parent" 
        android:layout_height="fill_parent">

     <TabWidget 
          android:id="@android:id/tabs" 
          android:layout_width="fill_parent" 
          android:layout_height="64dip" />

     <FrameLayout 
          android:id="@android:id/tabcontent" 
          android:layout_width="fill_parent" 
          android:layout_height="fill_parent"
          android:paddingTop="75dip">  

          <ListView
              android:id = "@+id/danhsach"
              android:layout_width="fill_parent" 
              android:layout_height="fill_parent" />

     </FrameLayout> 
</TabHost>


for(int i=0; i<5; i++ )
{
    final TabSpec x=tabHost2.newTabSpec("x");
    View row = inflater.inflate(R.layout.indicator1,null);
    final TextView indicator1 =(TextView) row.findViewById(R.id.textView_indicator1);
    indicator1.setText(indicator_list[i]);
    // indicator1.setShadowLayer(1, 0, 1, 0xFF013201);

    x.setIndicator(row);

    x.setContent(new TabContentFactory() {
        public View createTabContent(String arg) {
        return gallery2;
        }
    });

    tabHost2.addTab(x);  
}

you can do like this.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜