最近在用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. 条款3:尽可能使用const(use const whenever possible)

    1.只要这(某值保持不变)是事实,就应该确实说出来,这样可以获得编译器的协助,确保这条约束不被违反. 2.keyword const 有很多种用法,但都简单易用. 2.1classes 外部修饰glo ...

  2. 事务场景中,抛出异常被catch后,如果需要回滚,一定要手动回滚事务

    Spring使用声明式事务处理,默认情况下,如果被注解的数据库操作方法中发生了unchecked异常,所有的数据库操作将rollback:如果发生的异常是checked异常,默认情况下数据库操作还是会 ...

  3. LeetCode(171) Excel Sheet Column Number

    题目 Related to question Excel Sheet Column Title Given a column title as appear in an Excel sheet, re ...

  4. python编程之API入门: (二)python3中使用新浪微博API

    回顾API使用的流程 通过百度地图API的使用,我理解API调用的一般流程为:生成API规定格式的url->通过urllib读取url中数据->对json格式的数据进行解析.下一步,开始研 ...

  5. Python+selenium常用方法(Webdriver API)

    小编整理了目前学习的Python+selenium常用的一些方法函数,以后有新增再随时更新. 加载浏览器驱动: webdriver.Firefox() 打开页面:get() 关闭浏览器:quit() ...

  6. HDU3183A Magic Lamp,和NYOJ最大的数一样

    A Magic Lamp Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Tot ...

  7. cp: omitting directory解决方案

    cp: omitting directory是因为目录下面还有目录.应该使用递归方法.需要加入-r参数. 及:cp -r 该目录名.

  8. 洛谷 P 1018 乘积最大 ==Codevs

    题目描述 今年是国际数学联盟确定的“2000――世界数学年”,又恰逢我国著名数学家华罗庚先生诞辰90周年.在华罗庚先生的家乡江苏金坛,组织了一场别开生面的数学智力竞赛的活动,你的一个好朋友XZ也有幸得 ...

  9. jquery的ajax提交时“加载中”提示的处理方法

    方法1:使用ajaxStart方法定义一个全局的“加载中...”提示 $(function(){    $("#loading").ajaxStart(function(){    ...

  10. .NET Core windows开发环境 + Git代码控管 + Docker 部署环境搭建

    开发环境准备 下载vs code,.NET Core sdk: https://www.microsoft.com/net/core#windowscmd 目前最新版为code 1.8.1,.NET ...