Android TabHost設置setCurrentTab(index),當index!=0時,默認加載第一個tab問題解決方法。


最近在用TabHost,默認希望顯示第2個tab,發現總是加載第三個tab的同時加載第一個,解決方法如下:

 1、首先查看addTab(TabSpec tabSpec)源代碼:

/**
     * Add a tab.
     * @param tabSpec Specifies how to create the indicator and content.
     */
    public void addTab(TabSpec tabSpec) {

        if (tabSpec.mIndicatorStrategy == null) {
            throw new IllegalArgumentException("you must specify a way to create the tab indicator.");
        }

        if (tabSpec.mContentStrategy == null) {
            throw new IllegalArgumentException("you must specify a way to create the tab content");
        }
        View tabIndicator = tabSpec.mIndicatorStrategy.createIndicatorView();
        tabIndicator.setOnKeyListener(mTabKeyListener);

        // If this is a custom view, then do not draw the bottom strips for
        // the tab indicators.
        if (tabSpec.mIndicatorStrategy instanceof ViewIndicatorStrategy) {
            mTabWidget.setStripEnabled(false);
        }
        mTabWidget.addView(tabIndicator);
        mTabSpecs.add(tabSpec);

        if (mCurrentTab == -1) {
            setCurrentTab(0);
        }
    }

  

 發現當我們進行addTab操作時,默認執行了最后一步,設置了第一個tab,所以我們需要bamCurrentTab的值設置為不為-1的一個數,且大於0。

2、再看setCurrentTab(int index)方法源碼:

public void setCurrentTab(int index) {
        if (index < 0 || index >= mTabSpecs.size()) {
            return;
        }

        if (index == mCurrentTab) {
            return;
        }

        // notify old tab content
        if (mCurrentTab != -1) {
            mTabSpecs.get(mCurrentTab).mContentStrategy.tabClosed();
        }

        mCurrentTab = index;
        final TabHost.TabSpec spec = mTabSpecs.get(index);

        // Call the tab widget's focusCurrentTab(), instead of just
        // selecting the tab.
        mTabWidget.focusCurrentTab(mCurrentTab);

        // tab content
        mCurrentView = spec.mContentStrategy.getContentView();

        if (mCurrentView.getParent() == null) {
            mTabContent.addView(mCurrentView, new ViewGroup.LayoutParams(
                                    ViewGroup.LayoutParams.MATCH_PARENT,
                                    ViewGroup.LayoutParams.MATCH_PARENT));
        }

        if (!mTabWidget.hasFocus()) {
            // if the tab widget didn't take focus (likely because we're in touch mode)
            // give the current tab content view a shot
            mCurrentView.requestFocus();
        }

        //mTabContent.requestFocus(View.FOCUS_FORWARD);
        invokeOnTabChangeListener();
    }

  

當mCurrentTab不為-1的時候會執行mTabSpecs.get(mCurrentTab).mContentStrategy.tabClosed()操作,所以在我們執行setCurrentTab()方法之前,我們再把mCurrentTab的值恢復為-1,這樣就不會執行關閉操作導致空指針異常。

3、具體方法如下:

 //取消tabhost默認加載第一個tab。
        try
        {
            Field current = tabHost.getClass().getDeclaredField("mCurrentTab");
            current.setAccessible(true);
            current.setInt(tabHost, 0);
        }catch (Exception e){
            e.printStackTrace();
        }

        TabHost.TabSpec tSpecCoupon = tabHost.newTabSpec("sth");
        tSpecCoupon.setIndicator(tabIndicator1);
        tSpecCoupon.setContent(new DummyTabContent(getBaseContext()));
        tabHost.addTab(tSpecCoupon);

        //mCurrentTab恢復到-1狀態
        try
        {
            Field current = tabHost.getClass().getDeclaredField("mCurrentTab");
            current.setAccessible(true);
            current.set(tabHost, -1);
        }catch (Exception e){
            e.printStackTrace();
        }

  

到此,我們屏蔽了默認的setCurrentTab(0)操作,同時恢復為-1后,又執行了我們的setCurrentTab(1)操作。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM