Activity
public class MainActivity implements TabHost.OnTabChangeListener {
private String tabId;
private FragmentTabHost tabHost;
private String[] tabText;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tabHost = (FragmentTabHost) findViewById(R.id.tabhost);
tabHost.setup(this, getSupportFragmentManager(), R.id.flContainer);
tabHost.getTabWidget().setDividerDrawable(null);
tabHost.setOnTabChangedListener(this);
initTab();
IntentTool.startVersionCheckService(this);
}
private void initTab() {
// 獲取tab昵稱
tabText = new String[]{getString(R.string.tab_refuel_title),
getString(R.string.tab_shop_title),
getString(R.string.tab_server_title),
getString(R.string.tab_my_title)};
// 獲取切換圖片
int[] imageRes = new int[]{R.drawable.tab_refuel,
R.drawable.tab_shop,
R.drawable.tab_server,
R.drawable.tab_my_center};
// 獲取切換的Fragments
Class[] fragments = new Class[]{
RefuelFragment.class,
ShopFragment.class,
ServerFragment.class,
MyFragment.class};
for (int i = 0; i < tabText.length; i++) {
View view;
if (i==0){
view = View.inflate(this,R.layout.item_tab_s,null);
}else if(i==1){
view = View.inflate(this,R.layout.item_tab_s1,null);
}else{
view = View.inflate(this,R.layout.item_tab,null);
}
((TextView) view.findViewById(R.id.tv)).setText(tabText[i]);
((ImageView) view.findViewById(R.id.iv)).setImageResource(imageRes[i]);
TabHost.TabSpec tabSpec = tabHost.newTabSpec(tabText[i]).setIndicator(view);
tabHost.addTab(tabSpec, fragments[i], null);
tabHost.setTag(i);
}
tabHost.getTabWidget().getChildTabViewAt(0).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
tabClick(0);
}
});
tabHost.getTabWidget().getChildTabViewAt(1).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
tabClick(1);
}
});
tabHost.getTabWidget().getChildTabViewAt(2).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
tabClick(2);
}
});
tabHost.getTabWidget().getChildTabViewAt(3).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
tabClick(3);
}
});
}
private void tabClick(int position){
if (position == 3){
setStatuBarStyle(false);
setStatusBarColor(R.color.kRedColor);
}else{
setStatuBarStyle(true);
setStatusBarColor(R.color.colorPrimary);
}
String tabStr = tabText[position];
if (tabStr.equals(tabId)){
// EventBus.getDefault().post(new TabRefreshEvent(tabId));
}else{
tabHost.setCurrentTab(position);
tabHost.getTabWidget().requestFocus(View.FOCUS_FORWARD);
}
}
@Override
public void onBackPressed() {
if (tabText[0].equals(tabId)){
finish();
}else{
tabClick(0);
}
}
@Override
public void onTabChanged(String s) {
tabId = s;
}
@Subscribe(threadMode = ThreadMode.MAIN)
public void onToHomeEvent(ToHomeEvent event){
String tag = event.getTag();
if (!TextUtils.isEmpty(tag)){
tabClick(Integer.parseInt(tag));
}else{
tabClick(0);
}
}
@Override
protected void onDestroy() {
super.onDestroy();
}
}
XML
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
android:orientation="vertical"
tools:context="com.android.mikk_eluyn.function.home.activity.MainActivity">
<FrameLayout
android:id="@+id/flContainer"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:background="@android:color/white">
</FrameLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@mipmap/img_line"
android:orientation="horizontal"/>
<android.support.v4.app.FragmentTabHost
android:id="@+id/tabhost"
android:layout_width="match_parent"
android:layout_height="49dp"
android:background="@color/titleBarBG">
</android.support.v4.app.FragmentTabHost>
</LinearLayout>
最后編輯于 :
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者