Mark, thanks that worked!!  For thread recycling, I will put the final
classes at the end.

Notes:
    - You need to shift mTabContent down by setting margins.  Used a
hard-coded 65.  Could not find a Static for this value.
    - Found a bug when I implemented my removeTab() method.  You
cannot just remove children from mTabWidget & mTabContent, because
mTabSpecs needs to also to be changed.  It is not accessable.
Therefore you must:
          - Go get all the views & titles that are not the one you are
trying to get rid of.
          - Call clearAllTabs()
          - Add the remaining views back.
The bug is, mTabWidget's mSelectedTab member does not get reset to 0.
If the first tab is not selected when the clear happens, then when you
try to add a tab, eventually mTabWidget's setCurrentTab() gets called,
causing a Null Pointer Exception on line 164.

//
==========================================================================
import android.content.Context;
import android.view.Gravity;
import android.widget.TabHost;
import android.widget.TabHost.TabContentFactory;
import android.widget.TabHost.TabSpec;
import android.widget.TabWidget;

class DynamicTabHost extends TabHost{
    public DynamicTabHost(Context context){
        super(context);

        TabWidget tabs = new TabWidget(super.getContext() );
        tabs.setId(android.R.id.tabs);
        addView(tabs, new LayoutParams
(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT,Gravity.TOP));

        FrameLayout frame = new FrameLayout(super.getContext() );
        frame.setId(android.R.id.tabcontent);
        FrameLayout.LayoutParams params = new LayoutParams
(LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT,Gravity.BOTTOM);
        params.setMargins(0, 65, 0, 0);
        addView(frame, params);

        super.setup();
    }

    public void addTab(View view, String tabTitle){
        TabSpec tabSpec = super.newTabSpec(tabTitle);
        tabSpec.setContent(new PreExistingViewFactory(view));
        tabSpec.setIndicator(tabTitle);

        super.addTab(tabSpec);
    }

    public void removeTab(View view){
        // determine where tab is to get rid of
        ViewGroup content = getTabContentView();
        int nukedIdx = content.indexOfChild(view);
        if (nukedIdx == -1) return;

        // prepare to get the remaining tab instances prior to
clearAllTabs()
        int nTabs = getTabCount();
        View[]   remainingViews  = null;
        String[] remainingTitles = null;

        if (nTabs > 1){
            remainingViews  = new View  [nTabs - 1];
            remainingTitles = new String[nTabs - 1];
            for (int i = 0, c = 0; i < nTabs; i++){
                if (i != nukedIdx){
                    remainingViews [c  ] = content.getChildAt(i);
                    remainingTitles[c++] = getTabTitle(i);
                }
            }
        }
        // only way to get state correct
        clearAllTabs();

        // add back any remaining
        for(int i = 0; i < nTabs - 1; i++){
            addTab(remainingViews[i], remainingTitles[i]);
        }
    }

    private String getTabTitle(int index){
        RelativeLayout tab = (RelativeLayout) super.getTabWidget
().getChildAt(index);
        TextView text = (TextView) tab.getChildAt(1);
        return text.getText().toString();
    }

    // needed to fix Bug in clearAllTabs; TabWidget corrupt
    @Override
    public void clearAllTabs(){
        // TabWidget corrupt when first not current
        super.setCurrentTab(0);
        super.clearAllTabs();
    }
}
//--------------------------------------------------------------------------
class PreExistingViewFactory implements TabContentFactory{
    private final View preExisting;

    protected PreExistingViewFactory(View view){
        preExisting = view;
    }
    public View createTabContent(String tag) {
        return preExisting;
    }
}

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers-unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to