adding tabs dynamically in android

三世轮回 提交于 2020-01-13 04:52:09

问题


in my application i want to add tabs dynamically ob click of button.how can i add? the rest gui shoul be same as it is.only tabs should be added. here is xml file-

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/Beige"
android:focusable="true"
android:orientation="vertical" >
    <LinearLayout
    android:id="@+id/linearLayout3"
    android:layout_width="match_parent"
    android:layout_height="50dp"
    android:layout_alignLeft="@+id/linearLayout2"
    android:layout_below="@+id/linearLayout2" >
    <TabHost
        android:id="@+id/th"
        android:layout_width="match_parent"
        android:layout_height="50dp" >
        <TabWidget
            android:id="@+id/tabs"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />

         <FrameLayout android:id="@+id/tabcontent"
          android:layout_width="fill_parent"
          android:layout_height="fill_parent"
            android:paddingTop="62px">
       <Button android:id="@+id/buttontab"
       android:layout_width="fill_parent"
       android:layout_height="fill_parent"
         android:text="A semi-random button"
        />
       </FrameLayout>        
    </TabHost>
</LinearLayout>
<LinearLayout
    android:id="@+id/linearLayout4"
    android:layout_width="match_parent"
    android:layout_height="380dp"
    android:layout_alignLeft="@+id/linearLayout3"
    android:layout_below="@+id/linearLayout3"
    android:orientation="vertical" >
    <ScrollView
        android:id="@+id/scrollView1"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >
        <HorizontalScrollView
            android:id="@+id/hv"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" >
            <ImageView
                android:id="@+id/imv"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" />
        </HorizontalScrollView>
    </ScrollView>
</LinearLayout>
 <LinearLayout
    android:layout_width="match_parent"
    android:layout_height="60dp"
    android:layout_alignLeft="@+id/linearLayout4"
    android:layout_below="@+id/linearLayout4" >
    <Button
        android:id="@+id/addtab"
        android:layout_width="80dp"
        android:layout_height="50dp"
        android:onClick="addtab"
        android:text="@string/addtab"
        android:textColor="@color/Black" />
  </LinearLayout>
  </RelativeLayout>

the rest scrollview ,horizontal scrollview should be same for each tab.

on click of button-

    public void addtab(View v){

    TabHost tabHost = new TabHost(this);
    tabHost=(TabHost) findViewById(R.id.th);

    TabWidget tabWidget = new TabWidget(this);
    tabWidget=(TabWidget) findViewById(R.id.tabs); 
    tabHost.addView(tabWidget);
    FrameLayout tabContent = new FrameLayout(this);
    tabContent=(FrameLayout) findViewById(R.id.tabcontent);
    tabContent.setPadding(0, 65, 0, 0);
    tabHost.addView(tabContent);
    TextView content = new TextView(this);
    content.setText("This is the Frame Content");
    content.setId(100);
    tabHost.setup();
    TabSpec tspec1 = tabHost.newTabSpec("Tab1");
    tspec1.setIndicator("Button");
    tspec1.setContent(new PreExistingViewFactory(content));
    tabHost.addTab(tspec1);     

    }

class PreExistingViewFactory implements TabContentFactory{
    private final View preExisting;
    protected PreExistingViewFactory(View view){
    preExisting = view;
    }
    public View createTabContent(String tag) {
    return preExisting;
    }
    }

how can add tabs?? please help me

exceptions-

02-18 16:02:14.375: E/AndroidRuntime(737): java.lang.IllegalStateException: Could not execute method of the activity
02-18 16:02:14.375: E/AndroidRuntime(737):  at android.view.View$1.onClick(View.java:2072)
02-18 16:02:14.375: E/AndroidRuntime(737):  at android.view.View.performClick(View.java:2408)
02-18 16:02:14.375: E/AndroidRuntime(737):  at android.view.View$PerformClick.run(View.java:8816)
02-18 16:02:14.375: E/AndroidRuntime(737):  at android.os.Handler.handleCallback(Handler.java:587)
02-18 16:02:14.375: E/AndroidRuntime(737):  at android.os.Handler.dispatchMessage(Handler.java:92)
02-18 16:02:14.375: E/AndroidRuntime(737):  at android.os.Looper.loop(Looper.java:123)
02-18 16:02:14.375: E/AndroidRuntime(737):  at android.app.ActivityThread.main(ActivityThread.java:4627)
02-18 16:02:14.375: E/AndroidRuntime(737):  at java.lang.reflect.Method.invokeNative(Native Method)
02-18 16:02:14.375: E/AndroidRuntime(737):  at java.lang.reflect.Method.invoke(Method.java:521)
02-18 16:02:14.375: E/AndroidRuntime(737):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
 02-18 16:02:14.375: E/AndroidRuntime(737):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
02-18 16:02:14.375: E/AndroidRuntime(737):  at dalvik.system.NativeStart.main(Native Method)
02-18 16:02:14.375: E/AndroidRuntime(737): Caused by: java.lang.reflect.InvocationTargetException
02-18 16:02:14.375: E/AndroidRuntime(737):  at com.my.zproject.Work.addtab(Work.java:169)
02-18 16:02:14.375: E/AndroidRuntime(737):  at java.lang.reflect.Method.invokeNative(Native Method)
02-18 16:02:14.375: E/AndroidRuntime(737):  at java.lang.reflect.Method.invoke(Method.java:521)
02-18 16:02:14.375: E/AndroidRuntime(737):  at android.view.View$1.onClick(View.java:2067)
02-18 16:02:14.375: E/AndroidRuntime(737):  ... 11 more
02-18 16:02:14.375: E/AndroidRuntime(737): Caused by: java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
02-18 16:02:14.375: E/AndroidRuntime(737):  at android.view.ViewGroup.addViewInner(ViewGroup.java:1970)

回答1:


override getPagetitle method in FragmentPagerAdapter as,

 @Override
public CharSequence getPageTitle(int position) {
    return myFragmentTitleList.get(position);
}

where myFragmentTitleList is your arraylist of titles.



来源:https://stackoverflow.com/questions/14933542/adding-tabs-dynamically-in-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!