溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

TabHost中跳轉(zhuǎn)到指定Tab頁(yè)問(wèn)題

發(fā)布時(shí)間:2020-07-23 01:19:40 來(lái)源:網(wǎng)絡(luò) 閱讀:708 作者:EX_潛力股 欄目:移動(dòng)開(kāi)發(fā)

最近在使用TabHost的時(shí)候遇到一個(gè)問(wèn)題:
TabHost添加了4個(gè)Activity作為tab頁(yè)面,我們從左至右的順序稱呼它們?yōu)閠ab1,tab2,tab3,tab4??墒敲看芜M(jìn)入TabHost頁(yè)面的時(shí)候,不管我進(jìn)來(lái)的時(shí)候點(diǎn)擊的是指向哪個(gè)Activity的跳轉(zhuǎn),tab1的Activity總會(huì)首先被執(zhí)行??墒俏蚁M男Ч牵尹c(diǎn)擊tab2的跳轉(zhuǎn),我就只希望執(zhí)行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);           
        }
    }
重點(diǎn)看最后兩句代碼,當(dāng)變量mCurrentTab 等于-1的時(shí)候,就setCurrentTab(0);然后再找到mCurrentTab 變量,發(fā)現(xiàn)它的聲明如下:
protected int mCurrentTab = -1;
通過(guò)上面的情況,我推測(cè)是因?yàn)樽兞縨CurrentTab 的賦值的情況,導(dǎo)致執(zhí)行addTab的方法的時(shí)候,會(huì)執(zhí)行setCurrentTab(0);方法,這樣第一個(gè)Activity就會(huì)被首先執(zhí)行。并且第一次調(diào)用addTab添加的Activity總會(huì)被執(zhí)行。
解決方法:
根據(jù)上面的情況,利用反射機(jī)制對(duì)TabHost 的變量mCurrentTab 的賦值進(jìn)行控制,就可以實(shí)現(xiàn)對(duì)于Activity的獨(dú)立訪問(wèn)。分為2步。
第一步:將mCurrentTab 的值改為非-1,這些代碼要在addTab方法調(diào)用之前寫(xiě),這樣防止addTab方法的最后兩句代碼執(zhí)行。如下:
                try
                {
                        Field idcurrent = tabHost.getClass()
                                        .getDeclaredField("mCurrentTab");
                        idcurrent.setAccessible(true);
                        idcurrent.setInt(tabHost, -2);
                }
                catch (Exception e)
                {
                        e.printStackTrace();
                }
第二步:在addTab方法執(zhí)行之后修改mCurrentTab 的值,這樣是為了調(diào)用setCurrentTab方法時(shí)正常執(zhí)行,如下:
                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();
                }
最后,把上述的整體的一個(gè)功能代碼寫(xiě)一下:
                //取得想跳轉(zhuǎn)到的的tab的Id
                Bundle extras = getIntent().getExtras();
                Resources resources = getResources();
                String defaultTab = extras.getString(STARTING_TAB);
                int tadid = defaultTab == null ? 2 : Integer.valueOf(defaultTab);
                //設(shè)置mCurrentTab為非-1,addtab時(shí)候不會(huì)進(jìn)入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));
                //設(shè)置mCurrentTab與tadid不同,并且不能數(shù)組越界(0-2),保證第一次進(jìn)入tab的setCurrentTab()方法正常運(yùn)行
                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();
                }
                //進(jìn)入傳來(lái)的選項(xiàng)卡
                tabHost.setCurrentTab(tadid);

說(shuō)了那么多,最重要的就是最后的這句話,前面的都是鋪墊,希望對(duì)大家有幫助!

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI