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

Android TabHost设置setCurrentTab(index),当index!=0时,默认加载第一个tab问题解决方法。的更多相关文章

  1. nginx 配置使用index.php作为目录的默认加载文件

    配置如下: 在server增加一行: index index.php index.html index.htm default.php default.htm default.html 增加后如下: ...

  2. eclipse中创建的spring-boot项目在启动时指定加载那一个配置文件的设置

    步骤如下:鼠标点击项目右键—>Run As—>Run Configurations—>Java Application (如下图) 鼠标右键点击Java Application——— ...

  3. android开发之Fragment加载到一个Activity中

    Fragments 是android3.0以后添加的.主要是为了方便android平板端的开发.方便适应不同大小的屏幕.此代码是为了最简单的Fragment的使用,往一个Activity中添加Frag ...

  4. 《ArcGIS Runtime SDK for Android开发笔记》——(12)、自定义方式加载Bundle格式缓存数据

    随着ArcGIS 10.3的正式发布,Esri推出了新的紧凑型缓存格式以增强用户的访问体验.新的缓存格式下,Esri将缓存的索引信息.bundlx包含在了缓存的切片文件.bundle中.具体如下图所示 ...

  5. Android利用V4包中的SwipeRefreshLayout实现上拉加载

    基本原理 上拉加载或者说滚动到底部时自动加载,都是通过判断是否滚动到了ListView或者其他View的底部,然后触发相应的操作,这里我们以 ListView来说明.因此我们需要在监听ListView ...

  6. android开发(29) 自定义曲线,可拖动,无限加载

    项目需要 做一个曲线,该曲线的数据时不断加载的.如下图,当不断向左拖动时,图形曲线要随着拖动移动,并在拖动到边界时需要加载更多数据. 先看步骤: 1.在Activity里放一个surfaceView ...

  7. Android学习记录(8)—Activity的四种加载模式及有关Activity横竖屏切换的问题

    Activity有四种加载模式:standard(默认), singleTop, singleTask和 singleInstance.以下逐一举例说明他们的区别: standard:Activity ...

  8. 【转】Sqlite 混合模式程序集是针对“v2.0.50727”版的运行时生成的,在没有配置其他信息的情况下,无法在 4.0 运行时中加载该...

    开发环境: vs2010+.net framework 4.0+ System.Data.SQLite.DLL (2.0)今天在做Sqlite数据库测试,一运行程序在一处方法调用时报出了一个异常 混合 ...

  9. [转]为什么Java中的HashMap默认加载因子是0.75

    前几天在一个群里看到有人讨论hashmap中的加载因子为什么是默认0.75. HashMap源码中的加载因子 static final float DEFAULT_LOAD_FACTOR = 0.75 ...

随机推荐

  1. js 将页面保存为图片

    <!DOCTYPE html><html><head><title>保存为images</title><meta charset=&q ...

  2. 条款24:若所有参数皆需要类型转换,请为此采用non-member函数(Declare non-member functions when type conversions should apply to all parameters)

    NOTE: 1.如果你需要为某个函数的所有参数(包括this指针所指的那个隐喻参数)进行类型转换,那么这个函数必须是个non-member.

  3. perl学习之:package and module

    perl的包(package)和模块(PM) ==================================包package===========================     pac ...

  4. Mysql ICP(翻译)

    英文版原文链接 https://mariadb.com/kb/en/library/index-condition-pushdown/ ICP 全称 Index Condition Pushdown. ...

  5. ACM模板(Java)

    模板 Trie HIHOCODER1014 static final int N = (int)1e5+10; static final int SIGMA=(int)27; static int c ...

  6. POJ 1161 Walls(Floyd , 建图)

    题意: 给定n个城市, 然后城市之间会有长城相连, 长城之间会围成M个区域, 有L个vip(每个vip会处于一个城市里)要找一个区域聚会, 问一共最少跨越多少个长城. 分析: 其实这题难就难在建图, ...

  7. 修改centos的yum源为国内的源

    1.安装Centos后默认的Yum源如下 ll /etc/yum.repos.d/   [root@localhost ~]# ll /etc/yum.repos.d/ total 32 -rw-r- ...

  8. Linux(5):正则表达式 & 权限

    正则表达式: 特殊符号: '' ---> 所见即所得,里面的内容都会被原封不动的输出出来 "" ---> 与单引号类似,但其中的特殊符号会被解析运行 `` ---> ...

  9. eclipse pom.xml 报错org.apache.maven.plugin.war.WarMojo的解决办法

    如题,maven项目eclipse提示pom.ml有错,提示信息就是org.apache.maven.plugin.war.WarMojo. 然后执行 maven install 出现如下错误提示 [ ...

  10. 使用windows操作EXCEL如何关闭EXCEL进程

    经常项目上有导入excel的需求,其实导入一个固定格式的excel数据非常容易,但是,发现一个问题就是,导入excel后,用户在打开excel时,必须要打开2次才能打开excel,这让人很不爽:开始查 ...