Android. Dynamically created tabs can only be clicked once.

I'm probably doing something silly ... I have one statically created tab and a bunch of dynamically created tabs on the same tabhost. The static tab is displayed by default. When I click on one of the dynamically created tabs, it loads the dynamic content correctly. But when I click on any other tab after that, no matter if it was dynamically or statically created, it doesn't do anything. Any idea why?

This is what I am trying to do in the MyActivitys onResume method:

final TabHost tabs = (TabHost)findViewById(R.id.tabHost);
tabs.setup();

// Tab with static content
TabHost.TabSpec instructions = tabs.newTabSpec("instructions");
tabs.setOnTabChangedListener(new TabHost.OnTabChangeListener() {
    @Override public void onTabChanged(String tabId) {
        setTabColors(tabs); 
    } 
});
instructions.setContent(R.id.instructions); 
instructions.setIndicator("Instructions");
tabs.addTab(instructions);

// Tabs with dynamic content
for (int i = 0; i < 5; i++) {
    TabHost.TabSpec category = tabs.newTabSpec("cat" + i);
    final boolean m_editable = editable;
    category.setContent(new TabHost.TabContentFactory() {
        public View createTabContent(String tag) {  
            ScrollView scrollView = new ScrollView(MyActivity.this);
            TableLayout dataMatrix = new TableLayout(MyActivity.this);                  
            final LayoutInflater inflater = LayoutInflater.from(MyActivity.this);   
            for(int i=0; i<2; i++) {
                TableRow item = (TableRow) inflater.inflate(R.layout.categorydata_entry, dataMatrix, false);
                ((EditText)(item.getChildAt(0))).setText("row"+i);
            }
            dataMatrix.addView(item,new TableLayout.LayoutParams( TableLayout.LayoutParams.FILL_PARENT, TableLayout.LayoutParams.WRAP_CONTENT));
        }
        scrollView.addView(dataMatrix);
        return scrollView;
    } 
});
category.setIndicator(i);
tabs.addTab(category);                      
    }               

      

And the corresponding element in layout.xml:

<TabHost android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:id="@+id/tabHost" > 
    <HorizontalScrollView
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content" 
        android:fillViewport="true" 
        android:scrollbars="none" >
        <TabWidget 
            android:id="@android:id/tabs" 
            android:layout_width="fill_parent" 
            android:layout_height="wrap_content" />
    </HorizontalScrollView>
    <FrameLayout 
        android:layout_width="fill_parent" 
        android:layout_height="fill_parent" 
        android:id="@android:id/tabcontent" >
        <LinearLayout 
            android:layout_width="fill_parent" 
            android:layout_height="wrap_content" 
            android:id="@+id/instructions" > 
            <TextView android:layout_width="fill_parent" 
                android:text="This is the static content tab" />
        </LinearLayout>
    </FrameLayout>          
</TabHost>

      

+3


source to share


1 answer


Finally I found my mistake. In case anyone else is facing the same difficulty: for whatever reason, the tabContent cannot be a ScrollView.

Placing a LinearLayout container around the scroll view will make everything work as expected.



+2


source







All Articles