Android TabHost设置setCurrentTab(index),当index!=0时,默认加载第一个tab问题解决方法。
最近在用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问题解决方法。的更多相关文章
- nginx 配置使用index.php作为目录的默认加载文件
配置如下: 在server增加一行: index index.php index.html index.htm default.php default.htm default.html 增加后如下: ...
- eclipse中创建的spring-boot项目在启动时指定加载那一个配置文件的设置
步骤如下:鼠标点击项目右键—>Run As—>Run Configurations—>Java Application (如下图) 鼠标右键点击Java Application——— ...
- android开发之Fragment加载到一个Activity中
Fragments 是android3.0以后添加的.主要是为了方便android平板端的开发.方便适应不同大小的屏幕.此代码是为了最简单的Fragment的使用,往一个Activity中添加Frag ...
- 《ArcGIS Runtime SDK for Android开发笔记》——(12)、自定义方式加载Bundle格式缓存数据
随着ArcGIS 10.3的正式发布,Esri推出了新的紧凑型缓存格式以增强用户的访问体验.新的缓存格式下,Esri将缓存的索引信息.bundlx包含在了缓存的切片文件.bundle中.具体如下图所示 ...
- Android利用V4包中的SwipeRefreshLayout实现上拉加载
基本原理 上拉加载或者说滚动到底部时自动加载,都是通过判断是否滚动到了ListView或者其他View的底部,然后触发相应的操作,这里我们以 ListView来说明.因此我们需要在监听ListView ...
- android开发(29) 自定义曲线,可拖动,无限加载
项目需要 做一个曲线,该曲线的数据时不断加载的.如下图,当不断向左拖动时,图形曲线要随着拖动移动,并在拖动到边界时需要加载更多数据. 先看步骤: 1.在Activity里放一个surfaceView ...
- Android学习记录(8)—Activity的四种加载模式及有关Activity横竖屏切换的问题
Activity有四种加载模式:standard(默认), singleTop, singleTask和 singleInstance.以下逐一举例说明他们的区别: standard:Activity ...
- 【转】Sqlite 混合模式程序集是针对“v2.0.50727”版的运行时生成的,在没有配置其他信息的情况下,无法在 4.0 运行时中加载该...
开发环境: vs2010+.net framework 4.0+ System.Data.SQLite.DLL (2.0)今天在做Sqlite数据库测试,一运行程序在一处方法调用时报出了一个异常 混合 ...
- [转]为什么Java中的HashMap默认加载因子是0.75
前几天在一个群里看到有人讨论hashmap中的加载因子为什么是默认0.75. HashMap源码中的加载因子 static final float DEFAULT_LOAD_FACTOR = 0.75 ...
随机推荐
- 【详●析】[GXOI/GZOI2019]逼死强迫症
[详●析][GXOI/GZOI2019]逼死强迫症 脑子不够用了... [题目大意] 在\(2\times N\)的方格中用\(N-1\)块\(2\times 1\)的方砖和\(2\)块\(1\tim ...
- 特殊权限和facl
目 录 第1章 FACL访问控制 1 1.1 FACL简介 1 1.2 getfacl命令查看acl权限 1 1.3 setfacl设置文件的cal权限 1 1.4 批量添加a ...
- POJ 2631 Roads in the North (求树的直径)
Description Building and maintaining roads among communities in the far North is an expensive busine ...
- mysql 常用命令(二)
1.创建数据库,并制定默认的字符集是utf8. CREATE DATABASE IF NOT EXISTS yourdbname DEFAULT CHARSET utf8 COLLATE utf8_g ...
- HDU1166-敌兵布阵,线段数模板题~~
敌兵布阵 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submi ...
- HDU 4821 字符串hash
题目大意: 希望找到连续的长为m*l的子串,使得m个l长的子串每一个都不一样,问能找到多少个这样的子串 简单的字符串hash,提前预处理出每一个长度为l的字符串的hash值 #include < ...
- [luoguP1168]中位数(主席树+离散化)
传送门 模板题一道,1A. ——代码 #include <cstdio> #include <algorithm> #define ls son[now][0], l, mid ...
- 【最长上升子序列记录路径(n^2)】HDU 1160 FatMouse's Speed
https://vjudge.net/contest/68966#problem/J [Accepted] #include<iostream> #include<cstdio> ...
- uva12558 Egyptian Fractions (HARD version)(迭代深搜)
Egyptian Fractions (HARD version) 题解:迭代深搜模板题,因为最小个数,以此为乐观估价函数来迭代深搜,就可以了. #include<cstdio> #inc ...
- Cloud BOS平台-自定义用户联系对象
适用业务场景:新增用户时,联系对象类型默认为:职员.客户.供应商.客户需要增加一类"承运商",类型选择"承运商"时,联系对象只显示相应的承运商."承运 ...