温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

TabHost中跳转到指定Tab页问题

发布时间:2020-07-23 01:19:40 来源:网络 阅读:691 作者:EX_潜力股 栏目:移动开发

最近在使用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);

说了那么多,最重要的就是最后的这句话,前面的都是铺垫,希望对大家有帮助!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI