TabHost中跳轉到指定Tab頁問題

最近在使用TabHost的時候遇到一個問題:
TabHost添加了4個Activity作爲tab頁面,我們從左至右的順序稱呼它們爲tab1,tab2,tab3,tab4。可是每次進入TabHost頁面的時候,不管我進來的時候點擊的是指向哪個Activity的跳轉,tab1的Activity總會首先被執行。可是我希望的效果是,我點擊tab2的跳轉,我就只希望執行tab2的Activity。
分析:我看了一下TabHost 2.1的源碼,找到addTab方法,如下所示。
    /**
     * 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.setDrawBottomStrips(false);
        }
        mTabWidget.addView(tabIndicator);
        mTabSpecs.add(tabSpec);

        if (mCurrentTab == -1) {     
            setCurrentTab(0);           
        }
    }
重點看最後兩句代碼,當變量mCurrentTab 等於-1的時候,就setCurrentTab(0);然後再找到mCurrentTab 變量,發現它的聲明如下:
protected int mCurrentTab = -1;
通過上面的情況,我推測是因爲變量mCurrentTab 的賦值的情況,導致執行addTab的方法的時候,會執行setCurrentTab(0);方法,這樣第一個Activity就會被首先執行。並且第一次調用addTab添加的Activity總會被執行。
解決方法:
根據上面的情況,利用反射機制對TabHost 的變量mCurrentTab 的賦值進行控制,就可以實現對於Activity的獨立訪問。分爲2步。
第一步:將mCurrentTab 的值改爲非-1,這些代碼要在addTab方法調用之前寫,這樣防止addTab方法的最後兩句代碼執行。如下:
                try
                {
                        Field idcurrent = tabHost.getClass()
                                        .getDeclaredField("mCurrentTab");
                        idcurrent.setAccessible(true);
                        idcurrent.setInt(tabHost, -2);
                }
                catch (Exception e)
                {
                        e.printStackTrace();
                }
第二步:在addTab方法執行之後修改mCurrentTab 的值,這樣是爲了調用setCurrentTab方法時正常執行,如下:
                try
                {
                        Field idcurrent = tabHost.getClass()
                                        .getDeclaredField("mCurrentTab");
                        idcurrent.setAccessible(true);
                        if (tadid == 0)
                        {
                                idcurrent.setInt(tabHost, 1);
                        }
                        else
                        {
                                idcurrent.setInt(tabHost, 0);
                        }
                }
                catch (Exception e)
                {
                        e.printStackTrace();
                }
最後,把上述的整體的一個功能代碼寫一下:
                //取得想跳轉到的的tab的Id
                Bundle extras = getIntent().getExtras();
                Resources resources = getResources();
                String defaultTab = extras.getString(STARTING_TAB);
                int tadid = defaultTab == null ? 2 : Integer.valueOf(defaultTab);
                //設置mCurrentTab爲非-1,addtab時候不會進入setCurrentTab()
                try
                {
                        Field idcurrent = tabHost.getClass()
                                        .getDeclaredField("mCurrentTab");
                        idcurrent.setAccessible(true);
                        idcurrent.setInt(tabHost, -2);
                }
                catch (Exception e)
                {
                        e.printStackTrace();
                }

                Intent Intent1= new Intent(this,Activity1.class);
                Intent1.putExtras(extras);
                tabHost.addTab(tabHost.newTabSpec(Intent1_TAB).setIndicator(
                                resources.getString(R.string.Intent1)).setContent(
                                Intent1));

                Intent Intent12= new Intent(this, Activity2.class);
                Intent12.putExtras(extras);
                tabHost.addTab(tabHost.newTabSpec(Intent12_TAB).setIndicator(
                                resources.getString(R.string.Intent12)).setContent(
                                Intent12));

                Intent Intent13= new Intent(this, Activity3.class);
                Intent13.putExtras(extras);
                tabHost.addTab(tabHost.newTabSpec(Intent13_TAB).setIndicator(
                                resources.getString(R.string.Intent13)).setContent(
                                Intent13));
                //設置mCurrentTab與tadid不同,並且不能數組越界(0-2),保證第一次進入tab的setCurrentTab()方法正常運行
                try
                {
                        Field idcurrent = tabHost.getClass()
                                        .getDeclaredField("mCurrentTab");
                        idcurrent.setAccessible(true);
                        if (tadid == 0)
                        {
                                idcurrent.setInt(tabHost, 1);
                        }
                        else
                        {
                                idcurrent.setInt(tabHost, 0);
                        }
        }
                catch (Exception e)
                {
                        e.printStackTrace();
                }
                //進入傳來的選項卡
                tabHost.setCurrentTab(tadid);

說了那麼多,最重要的就是最後的這句話,前面的都是鋪墊,希望對大家有幫助!

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章