【核心代码】
package com.xzh.ui; import android.app.AlertDialog; import android.app.AlertDialog.Builder; import android.app.TabActivity; import android.content.Context; import android.content.DialogInterface; import android.content.Intent; import android.net.ConnectivityManager; import android.net.NetworkInfo.State; import android.os.Bundle; import android.view.GestureDetector; import android.view.GestureDetector.SimpleOnGestureListener; import android.view.KeyEvent; import android.view.MotionEvent; import android.view.View; import android.view.View.OnClickListener; import android.view.Window; import android.widget.FrameLayout; import android.widget.RadioButton; import android.widget.TabHost; import android.widget.TabHost.OnTabChangeListener; import android.widget.TabWidget; import android.widget.ViewFlipper; import com.example.livinghelper.R; import com.xzh.utils.AnimationTabHostUtils; import com.xzh.utils.CustomerDialog; import com.xzh.utils.ExitManager; /*本类主要实现和底部导航条tabhost,实现视图却换*/ public class MainActivity extends TabActivity implements OnTabChangeListener { public static MainActivity instance; private TabHost tabHost; private RadioButton main_tab_traffic, main_tab_living, main_tab_property, main_tab_wiki, main_tab_more; private GestureDetector gestureDetector;//手势滑动 private AnimationTabHostUtils mTabHostUtils; private TabWidget mTabWidget; private FrameLayout frameLayout; private ViewFlipper viewFlipper; /** 记录当前分页ID */ private int currentTabID = 0; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); //getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN); this.requestWindowFeature(Window.FEATURE_NO_TITLE); setContentView(R.layout.activity_main); instance=this; //检测网络情况 CheckNetwork(); mTabHostUtils = (AnimationTabHostUtils) findViewById(android.R.id.tabhost); //初始化底部的tabhost initTab(); init(); mTabWidget = (TabWidget) findViewById(android.R.id.tabs); System.out.println(mTabWidget.getChildCount() "======mTabWidget.getChildCount()====="); tabHost.setOnTabChangedListener(this); onTabChanged("traffic"); //手势滑动事件处理 gestureDetector = new GestureDetector(new TabHostTouch()); new View.OnTouchListener() { public boolean onTouch(View v, MotionEvent event) { if (gestureDetector.onTouchEvent(event)) { return true; } return false; } }; frameLayout = mTabHostUtils.getTabContentView(); ExitManager.getInstance().addActivity(this); } //初始化按钮 public void init(){ main_tab_traffic=(RadioButton)findViewById(R.id.main_tab_traffic); main_tab_property = (RadioButton) findViewById(R.id.main_tab_property); main_tab_living = (RadioButton) findViewById(R.id.main_tab_living); main_tab_wiki = (RadioButton) findViewById(R.id.main_tab_wiki); main_tab_more = (RadioButton) findViewById(R.id.main_tab_more); //主页 main_tab_traffic.setOnClickListener(new OnClickListener() { public void onClick(View view) { tabHost.setCurrentTabByTag("traffic"); } }); //理财 main_tab_property.setOnClickListener(new OnClickListener() { public void onClick(View view) { tabHost.setCurrentTabByTag("property"); } }); //生活 main_tab_living.setOnClickListener(new OnClickListener() { public void onClick(View view) { tabHost.setCurrentTabByTag("living"); } }); //百科 main_tab_wiki.setOnClickListener(new OnClickListener() { public void onClick(View view) { tabHost.setCurrentTabByTag("wiki"); } }); //更多 main_tab_more.setOnClickListener(new OnClickListener() { public void onClick(View view) { tabHost.setCurrentTabByTag("more"); } }); } //初始化底部的tab表 public void initTab(){ tabHost=getTabHost(); System.out.println(tabHost "===tabHost=="); tabHost.addTab(tabHost.newTabSpec("traffic").setIndicator("traffic") .setContent(new Intent(this, MainActivityTraffic.class))); tabHost.addTab(tabHost.newTabSpec("property").setIndicator("property") .setContent(new Intent(this, MainActivityProperty.class))); tabHost.addTab(tabHost.newTabSpec("living").setIndicator("living") .setContent(new Intent(this, MainActivityLiving.class))); tabHost.addTab(tabHost.newTabSpec("wiki").setIndicator("wiki") .setContent(new Intent(this, MainActivityWiki.class))); tabHost.addTab(tabHost.newTabSpec("more").setIndicator("more") .setContent(new Intent(this, MainActivityChat.class))); mTabHostUtils.setOpenAnimation(true); } //自定义触摸响应 private class TabHostTouch extends SimpleOnGestureListener { /** 滑动翻页所需距离 */ private static final int ON_TOUCH_DISTANCE = 80; @Override public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) { if (e1.getX() - e2.getX() <= (-ON_TOUCH_DISTANCE)) { currentTabID = mTabHostUtils.getCurrentTab() - 1; // tabHost.setCurrentTab(currentTabID-1<0?0:currentTabID-1); if (currentTabID < 0) { currentTabID = mTabHostUtils.getTabCount() - 1; } } else if (e1.getX() - e2.getX() >= ON_TOUCH_DISTANCE) { currentTabID = mTabHostUtils.getCurrentTab() 1; if (currentTabID >= mTabHostUtils.getTabCount()) { currentTabID = 0; } } tabHost=getTabHost(); tabHost.setCurrentTab(currentTabID); return false; } } @Override public boolean dispatchTouchEvent(MotionEvent event) { if (gestureDetector.onTouchEvent(event)) { event.setAction(MotionEvent.ACTION_CANCEL); } return super.dispatchTouchEvent(event); } //使用手势滑动的时候,执行本方法 @Override public void onTabChanged(String tabId) { if(tabId.equals("traffic")){ main_tab_traffic.setBackgroundResource(R.drawable.home_btn_bg_s); main_tab_living.setBackgroundResource(R.drawable.head_bg); main_tab_property.setBackgroundResource(R.drawable.head_bg); main_tab_wiki.setBackgroundResource(R.drawable.head_bg); main_tab_more.setBackgroundResource(R.drawable.head_bg); }else if(tabId.equals("living")){ main_tab_living.setBackgroundResource(R.drawable.home_btn_bg_s); main_tab_property.setBackgroundResource(R.drawable.head_bg); main_tab_wiki.setBackgroundResource(R.drawable.head_bg); main_tab_more.setBackgroundResource(R.drawable.head_bg); main_tab_traffic.setBackgroundResource(R.drawable.head_bg); }else if(tabId.equals("property")){ main_tab_property.setBackgroundResource(R.drawable.home_btn_bg_s); main_tab_wiki.setBackgroundResource(R.drawable.head_bg); main_tab_more.setBackgroundResource(R.drawable.head_bg); main_tab_traffic.setBackgroundResource(R.drawable.head_bg); main_tab_living.setBackgroundResource(R.drawable.head_bg); }else if(tabId.equals("wiki")){ main_tab_wiki.setBackgroundResource(R.drawable.home_btn_bg_s); main_tab_more.setBackgroundResource(R.drawable.head_bg); main_tab_traffic.setBackgroundResource(R.drawable.head_bg); main_tab_living.setBackgroundResource(R.drawable.head_bg); main_tab_property.setBackgroundResource(R.drawable.head_bg); }else{ main_tab_more.setBackgroundResource(R.drawable.home_btn_bg_s); main_tab_traffic.setBackgroundResource(R.drawable.head_bg); main_tab_living.setBackgroundResource(R.drawable.head_bg); main_tab_property.setBackgroundResource(R.drawable.head_bg); main_tab_wiki.setBackgroundResource(R.drawable.head_bg); } //由于采用下面的方法出现不能将String-->int类型,只能用上面这种笨 /*int tabID = Integer.valueOf(tabId); System.out.println(tabId "====tabId====="); for (int i = 0; i < mTabWidget.getChildCount(); i ) { if (i == tabID) { mTabWidget.getChildAt(i).setBackgroundResource( R.drawable.home_btn_bg_s); } else { mTabWidget.getChildAt(i).setBackgroundResource( R.drawable.head_bg); } }*/ } //退出程序 public boolean dispatchKeyEvent( KeyEvent event) { if (event.getAction()==KeyEvent.ACTION_DOWN&&event.getKeyCode()==KeyEvent.KEYCODE_BACK) { if (event.getRepeatCount() == 0) { CustomerDialog.Builder alertDialog = new CustomerDialog.Builder( MainActivity.this); alertDialog.setIcon(R.drawable.ic_menu_question); alertDialog.setTitle("是否退出程序"); alertDialog.setMessage("是否确定退出本程序 !"); alertDialog.setPositiveButton("确定", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { ExitManager.getInstance().exit(); android.os.Process.killProcess(android.os.Process.myPid()); } }); alertDialog.setNegativeButton("取消", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { dialog.dismiss(); } }); alertDialog.create().show(); } } return super.dispatchKeyEvent(event); } //没有网络的时候跳转到设置界面 public boolean CheckNetwork() { boolean flag = false; ConnectivityManager connManager = (ConnectivityManager) this.getSystemService(Context.CONNECTIVITY_SERVICE); //检测3g网络是否可用 State mobile = connManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE).getState(); //检测wifi是否可用 State wifi = connManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI).getState(); if (connManager.getActiveNetworkInfo() != null) flag = connManager.getActiveNetworkInfo().isAvailable();//如果得到的network可用 else { Builder b = new AlertDialog.Builder(this).setTitle("没有可用的网络") .setMessage("请开启GPRS或WIFI"); b.setPositiveButton("设置", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { //进入无线网络配置界面 Intent intent = new Intent("android.settings.WIRELESS_SETTINGS"); startActivity(intent); } }).setNeutralButton("取消", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { dialog.cancel(); } }).create(); b.show(); } return flag; } }