问题描述以下: java
我如今有一个TabHost ,装在的页面从左到右依次是Aactivity,Bactivity,Cactivity,Dactivity,Eactivity。 当我指定要跳转到 B, C, D 或者E Aactivity时候,tabHost老是都会先去加载A页面,这并非我想要的,我想要的是指定哪一个页面就去加载哪一个页面。 数组
经过查看源代码发现,咱们在tabHost.addTab(spec);的时候,源码以下: this
/** * 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); } }
注意查看这段代码 spa
if (mCurrentTab == -1) {
setCurrentTab(0);
} code
找到mCurrentTab的定义 ip
protected int mCurrentTab = -1; 默认为-1,全部咱们在添加切换卡页面的时候,老是会默认先执行 setCurrentTab(0); 这就是为何Tabhost老是会先默认加载第一个页面的缘由。 ci
找到问题就好办了,咱们能够在tabHost.addTab(spec);方法以前,经过反射机制对Tabhost的变量 mCurrentTab进行赋值。 get
Field mCurrentTab = null; try { mCurrentTab = tabHost.getClass() .getDeclaredField("mCurrentTab"); mCurrentTab.setAccessible(true); mCurrentTab.setInt(tabHost, -2); } catch (SecurityException e) { e.printStackTrace(); } catch (IllegalArgumentException e) { e.printStackTrace(); } catch (NoSuchFieldException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); }
mCurrentTab.setInt(tabHost, -2); 咱们对mCurrentTab进行赋值只要mCurrent的值不等于-1就行。在咱们添加完全部的activity以后。还有一次对mCurrentTab进行赋值操做。不然会报错 源码
try { if (mtabIndex == 0) { mCurrentTab.setInt(tabHost, 1); } else { mCurrentTab.setInt(tabHost, 0); } } catch (Exception e){ e.printStackTrace(); }
缘由以下; it
当咱们执行 tabHost.setCurrentTab(mtabIndex); //设置默认显示界面 查看源码以下:
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(); }
从上面代码中有2个地方咱们须要注意的是
if (index == mCurrentTab) {
return;
}
注意 传入的index不能和mCurrentTab值相等,不然直接就return了
// notify old tab content
if (mCurrentTab != -1) {
mTabSpecs.get(mCurrentTab).mContentStrategy.tabClosed();
}
注意红色部分 mCurrentTab不能小于0 而且不能大于等于mTabSpecs 的size() 不然就会数组越界。
这就是为何会在最后对mCurrentTab进行从新赋值的缘由
下面是一个连续代码以下:
Field mCurrentTab = null; try { mCurrentTab = tabHost.getClass() .getDeclaredField("mCurrentTab"); mCurrentTab.setAccessible(true); mCurrentTab.setInt(tabHost, -2); } catch (SecurityException e) { e.printStackTrace(); } catch (IllegalArgumentException e) { e.printStackTrace(); } catch (NoSuchFieldException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } // tabActivity实施了跳转界面 intent = new Intent().setClass(this, AActivity.class); spec = tabHost.newTabSpec("A").setIndicator(disCoverTab).setContent(intent); tabHost.addTab(spec); intent = new Intent().setClass(this, BActivity.class); spec = tabHost.newTabSpec("B").setIndicator(experienceTab).setContent(intent); tabHost.addTab(spec); intent = new Intent().setClass(this, CActivity.class); spec = tabHost.newTabSpec("C").setIndicator(homeTab).setContent(intent); tabHost.addTab(spec); intent = new Intent().setClass(this, DActivity.class); spec = tabHost.newTabSpec("D").setIndicator(tab3).setContent(intent); tabHost.addTab(spec); intent = new Intent().setClass(this, EActivity.class); spec = tabHost.newTabSpec("E").setIndicator(tab5).setContent(intent); tabHost.addTab(spec); try { if (mtabIndex == 0) { mCurrentTab.setInt(tabHost, 1); } else { mCurrentTab.setInt(tabHost, 0); } } catch (Exception e){ e.printStackTrace(); } tabHost.setCurrentTab(mtabIndex); //设置默认显示界面