最近在用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)操作。

更多相关文章

  1. 软件安装:android studio安装以及操作手册
  2. robotium原理之获取WebElement元素
  3. 如何修改android 默认的actionbar(Theme)
  4. android中SQLite的操作
  5. Android需要提升权限的操作方法
  6. 永久破解Tattoo root权限
  7. Android(安卓)sdcard 操作
  8. 在cmd窗口中查询android的sqlite3数据库表之步骤
  9. [RK3399][Android7.1] 调试笔记 --- 默认禁止弹出USB权限对话框

随机推荐

  1. Android——六大基本布局总结
  2. 《老罗Android第二季》Bitmap位图解码
  3. Android Frameworks系列(二) 彻底弄懂sta
  4. Android当前任务管理器不显示应用进程
  5. android 系统中的时区设置
  6. 在Android原生应用中嵌入React Native
  7. [Android]带你了解 Android 约束布局 Con
  8. OpenCV+ADT 第一个基于Android的OpenCV D
  9. Basic4android:多功能的Android应用软件快
  10. 关于Android设置控件margin无效的解决办