布局中的引用
<com.itheima.oschina.base.MyFragmentTabHost
android:id="@+id/tab"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="4dip"/>
自定义的MyFragmentTabHost
public class MyFragmentTabHost extends FragmentTabHost {
private String mCurrentTag;
private String mNoTabChangedTag;
public MyFragmentTabHost(Context context, AttributeSet attrs) {
super(context, attrs);
}
@Override
public void onTabChanged(String tag) {
Log.e("测试","tag:" + tag);
Log.e("测试","mCurrentTag:" + mCurrentTag);
Log.e("测试","mNoTabChangedTag:" + mNoTabChangedTag);
if (tag.equals(mNoTabChangedTag)) {
setCurrentTabByTag(mCurrentTag);
} else {
super.onTabChanged(tag);
mCurrentTag = tag;
}
}
public void setNoTabChangedTag(String tag) {
this.mNoTabChangedTag = tag;
}
}
枚举(用来提供TabHost的内容)
public enum MainTab {
NEWS(0, R.string.main_tab_name_news, R.drawable.tab_icon_new, ComprehensiveFragment.class),
TWEET(0, R.string.main_tab_name_tweet, R.drawable.tab_icon_tweet, TweetsViewPagerFragment.class),
QUICK(0, R.string.main_tab_name_quick, R.drawable.tab_icon_new, null),
EXPLORE(0, R.string.main_tab_name_explore, R.drawable.tab_icon_explore, FindFragment.class),
ME(0, R.string.main_tab_name_my, R.drawable.tab_icon_me, MyInformationFragment.class);
private int id;
private int resName;
private int resIcon;
private Class<?> clz;
private MainTab(int id, int resName, int resIcon, Class<?> clz) {
this.id = id;
this.resName = resName;
this.resIcon = resIcon;
this.clz = clz;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public int getResName() {
return resName;
}
public void setResName(int resName) {
this.resName = resName;
}
public int getResIcon() {
return resIcon;
}
public void setResIcon(int resIcon) {
this.resIcon = resIcon;
}
public Class<?> getClz() {
return clz;
}
public void setClz(Class<?> clz) {
this.clz = clz;
}
}
在Activity中的用法
private MyFragmentTabHost mTabHost
private void intiBottom() {
// 设置底部栏
mTabHost = (MyFragmentTabHost) findViewById(R.id.tab)
mTabHost.setup(this,getSupportFragmentManager(),R.id.content)
if (Build.VERSION.SDK_INT > 10) {
mTabHost.getTabWidget().setShowDividers(0)
# initTabs();
mTabHost.setCurrentTab(0)
mTabHost.setOnTabChangedListener(this)
}
ImageView showQuickOption = (ImageView) findViewById(R.id.quick_option_iv)
showQuickOption.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
showQuickOption()
}
})
}
#initTabs
private void initTabs() {
MainTab[] tabs = MainTab.values()
int size = tabs.length
for (int i = 0
MainTab mainTab = tabs[i]
TabHost.TabSpec tab = mTabHost.newTabSpec(getString(mainTab.getResName()))
View view = LayoutInflater.from(getApplicationContext()).inflat(R.layout.tab_indicator, null)
TextView title = (TextView) view.findViewById(R.id.tab_title)
Drawable drawable = this.getResources().getDrawable(mainTab.getResIcon())
title.setCompoundDrawablesWithIntrinsicBounds(null,drawable,null,null)
if (i == 2){
view.setVisibility(View.INVISIBLE)
mTabHost.setNoTabChangedTag(getString(mainTab.getResName()))
}
title.setText(getString(mainTab.getResName()))
tab.setIndicator(view)
tab.setContent(new TabHost.TabContentFactory() {
@Override
public View createTabContent(String tag) {
return new View(HomeActivity.this)
}
})
mTabHost.addTab(tab,mainTab.getClz(),null)
}
}
#onTabChanged
@Override
public void onTabChanged(String tabId) {
int size = mTabHost.getTabWidget().getTabCount()
for (int i = 0
View view = mTabHost.getTabWidget().getChildAt(i)
if (i == mTabHost.getCurrentTab()){
view.setSelected(true)
}else{
view.setSelected(false)
}
}
}