最近在使用TabHost的時(shí)候遇到一個(gè)問題:
TabHost添加了4個(gè)Activity作為tab頁面,我們從左至右的順序稱呼它們?yōu)閠ab1,tab2,tab3,tab4??墒敲看芜M(jìn)入TabHost頁面的時(shí)候,不管我進(jìn)來的時(shí)候點(diǎn)擊的是指向哪個(gè)Activity的跳轉(zhuǎn),tab1的Activity總會首先被執(zhí)行。可是我希望的效果是,我點(diǎn)擊tab2的跳轉(zhuǎn),我就只希望執(zhí)行tab2的Activity。
分析:我看了一下TabHost 2.1的源碼,找到addTab方法,如下所示。
/**
* Add a tab.
* @param tabSpec Specifies how to create the indicator and content.
*/
public void addTab(TabSpec tabSpec) {
成都創(chuàng)新互聯(lián)公司是一家專業(yè)提供鄂州企業(yè)網(wǎng)站建設(shè),專注與成都網(wǎng)站制作、做網(wǎng)站、html5、小程序制作等業(yè)務(wù)。10年已為鄂州眾多企業(yè)、政府機(jī)構(gòu)等服務(wù)。創(chuàng)新互聯(lián)專業(yè)網(wǎng)站制作公司優(yōu)惠進(jìn)行中。
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.setDrawBottomStrips(false);
}
mTabWidget.addView(tabIndicator);
mTabSpecs.add(tabSpec);
if (mCurrentTab == -1) {
setCurrentTab(0);
}
}
重點(diǎn)看最后兩句代碼,當(dāng)變量mCurrentTab 等于-1的時(shí)候,就setCurrentTab(0);然后再找到mCurrentTab 變量,發(fā)現(xiàn)它的聲明如下:
protected int mCurrentTab = -1;
通過上面的情況,我推測是因?yàn)樽兞縨CurrentTab 的賦值的情況,導(dǎo)致執(zhí)行addTab的方法的時(shí)候,會執(zhí)行setCurrentTab(0);方法,這樣第一個(gè)Activity就會被首先執(zhí)行。并且第一次調(diào)用addTab添加的Activity總會被執(zhí)行。
解決方法:
根據(jù)上面的情況,利用反射機(jī)制對TabHost 的變量mCurrentTab 的賦值進(jìn)行控制,就可以實(shí)現(xiàn)對于Activity的獨(dú)立訪問。分為2步。
第一步:將mCurrentTab 的值改為非-1,這些代碼要在addTab方法調(diào)用之前寫,這樣防止addTab方法的最后兩句代碼執(zhí)行。如下:
try
{
Field idcurrent = tabHost.getClass()
.getDeclaredField("mCurrentTab");
idcurrent.setAccessible(true);
idcurrent.setInt(tabHost, -2);
}
catch (Exception e)
{
e.printStackTrace();
}
第二步:在addTab方法執(zhí)行之后修改mCurrentTab 的值,這樣是為了調(diào)用setCurrentTab方法時(shí)正常執(zhí)行,如下:
try
{
Field idcurrent = tabHost.getClass()
.getDeclaredField("mCurrentTab");
idcurrent.setAccessible(true);
if (tadid == 0)
{
idcurrent.setInt(tabHost, 1);
}
else
{
idcurrent.setInt(tabHost, 0);
}
}
catch (Exception e)
{
e.printStackTrace();
}
最后,把上述的整體的一個(gè)功能代碼寫一下:
//取得想跳轉(zhuǎn)到的的tab的Id
Bundle extras = getIntent().getExtras();
Resources resources = getResources();
String defaultTab = extras.getString(STARTING_TAB);
int tadid = defaultTab == null ? 2 : Integer.valueOf(defaultTab);
//設(shè)置mCurrentTab為非-1,addtab時(shí)候不會進(jìn)入setCurrentTab()
try
{
Field idcurrent = tabHost.getClass()
.getDeclaredField("mCurrentTab");
idcurrent.setAccessible(true);
idcurrent.setInt(tabHost, -2);
}
catch (Exception e)
{
e.printStackTrace();
}
Intent Intent1= new Intent(this,Activity1.class);
Intent1.putExtras(extras);
tabHost.addTab(tabHost.newTabSpec(Intent1_TAB).setIndicator(
resources.getString(R.string.Intent1)).setContent(
Intent1));
Intent Intent12= new Intent(this, Activity2.class);
Intent12.putExtras(extras);
tabHost.addTab(tabHost.newTabSpec(Intent12_TAB).setIndicator(
resources.getString(R.string.Intent12)).setContent(
Intent12));
Intent Intent13= new Intent(this, Activity3.class);
Intent13.putExtras(extras);
tabHost.addTab(tabHost.newTabSpec(Intent13_TAB).setIndicator(
resources.getString(R.string.Intent13)).setContent(
Intent13));
//設(shè)置mCurrentTab與tadid不同,并且不能數(shù)組越界(0-2),保證第一次進(jìn)入tab的setCurrentTab()方法正常運(yùn)行
try
{
Field idcurrent = tabHost.getClass()
.getDeclaredField("mCurrentTab");
idcurrent.setAccessible(true);
if (tadid == 0)
{
idcurrent.setInt(tabHost, 1);
}
else
{
idcurrent.setInt(tabHost, 0);
}
}
catch (Exception e)
{
e.printStackTrace();
}
//進(jìn)入傳來的選項(xiàng)卡
tabHost.setCurrentTab(tadid);
說了那么多,最重要的就是最后的這句話,前面的都是鋪墊,希望對大家有幫助!
當(dāng)前名稱:TabHost中跳轉(zhuǎn)到指定Tab頁問題
網(wǎng)站地址:http://www.rwnh.cn/article2/jsddoc.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供微信公眾號、建站公司、搜索引擎優(yōu)化、定制網(wǎng)站、做網(wǎng)站、企業(yè)建站
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)