Android TabHost设置不加载第一个tab

      比如项目中有四个tab,但无论setcurrent(2或者3)第一个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,所以我们须要改变CurrentTab的值,设置为不为-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

        // ing 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 idcurrent = tabHost.getClass().getDeclaredField("mCurrentTab");

            idcurrent.setAccessible(true);

            idcurrent.setInt(tabHost, 0);

        }catch (Exception e){

            e.printStackTrace();

        }


      
        // 添加tab
        TabHost.TabSpec tSpecCoupon = tabHost.newTabSpec("sth");

        tSpecCoupon.setIndicator(tabIndicator1);

        tSpecCoupon.setContent(new DummyTabContent(getBaseContext()));

        tabHost.addTab(tSpecCoupon);



        //再把mCurrentTab恢复到-1状况
        try

        {

            Field idcurrent = tabHost.getClass().getDeclaredField("mCurrentTab");

            idcurrent.setAccessible(true);

            idcurrent.set(tabHost, -1);

        }catch (Exception e){

            e.printStackTrace();

        }

    

       tabHost.setCurrentTab(index);

    具体思路是:在添加tab前,把mCurrentTab的值改成 != -1,然后在添加tab后,把mCurrentTab的值改回 -1,最后再

                          setCurrentTab(index)。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值