参考 http://wenku.baidu.com/view/35bd14482b160b4e767fcfed.html
http://www.apkbus.com/android-14411-1-1.html
-----------------------------------------------------------------------------------
main_tab.xml
<?xml version="1.0" encoding="utf-8"?>
<xxxxxxxxxxxxxx.MyTabHost
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/tabhost" android:background="@null"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1" />
<TabWidget
android:id="@android:id/tabs"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@drawable/main_tab_bg"
/>
</LinearLayout>
</xxxxxxxxxxxxxx.MyTabHost>
-----------------------------------------------------------------------------------
MainTab(Activity).java
public class MainTab extends TabActivity {
public static final int TAB_MAIN = 0;
public static final int TAB_CLASS = 1;
public static final int TAB_CART = 2;
public static final int TAB_MORE = 3;
public static int mtabIndex = 0;
public static final String EXTRA_INDEX = "com.kugame.activity.MAIN_TAB_INDEX";
private static MyTabHost tabHost;
public static boolean LOGIN = false;
// 手势切换探测器
private static final int SWIPE_MIN_DISTANCE = 120;
private static final int SWIPE_MAX_OFF_PATH = 250;
private static final int SWIPE_THRESHOLD_VELOCITY = 200;
private GestureDetector gestureDetector;
View.OnTouchListener gestureListener;
private ViewFlipper viewFlipper;
int currentView = 0;
private static int maxTabIndex = 4;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getWindow().requestFeature(Window.FEATURE_NO_TITLE);
// getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
// WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.main_tab);
initViews();
}
@Override
protected void onResume() {
// onThemeChange();
super.onResume();
}
public static void setTab(Activity act, int tabIndex) {
if (tabIndex != 0) {
mtabIndex = tabIndex;
LOGIN = true;
Intent i = new Intent(act, MainTab.class);
act.startActivity(i);
} else {
mtabIndex = tabIndex;
Intent i = new Intent(act, MainTab.class);
act.startActivity(i);
}
}
public static void startActivity(Context context) {
Intent i = new Intent(context, MainTab.class);
context.startActivity(i);
}
private void initViews() {
tabHost = (MyTabHost) findViewById(android.R.id.tabhost);
TabHost.TabSpec spec;
Intent intent;
View tabs = LayoutInflater.from(this).inflate(R.layout.main_tabwidget,
null, false);
View tab1 = tabs.findViewById(R.id.tab1);
((ViewGroup) tab1.getParent()).removeView(tab1);
View tab2 = tabs.findViewById(R.id.tab2);
((ViewGroup) tab2.getParent()).removeView(tab2);
View tab3 = tabs.findViewById(R.id.tab3);
((ViewGroup) tab3.getParent()).removeView(tab3);
View tab4 = tabs.findViewById(R.id.tab4);
((ViewGroup) tab4.getParent()).removeView(tab4);
View tab5 = tabs.findViewById(R.id.tab5);
((ViewGroup) tab5.getParent()).removeView(tab5);
// tabActivity实施了跳转界面
intent = new Intent().setClass(this, NewsActivity.class);
spec = tabHost.newTabSpec("新闻").setIndicator(tab1).setContent(intent);
tabHost.addTab(spec);
intent = new Intent().setClass(this, GovernmentActivity.class);
spec = tabHost.newTabSpec("政务").setIndicator(tab2).setContent(intent);
tabHost.addTab(spec);
intent = new Intent().setClass(this, PsncenterActivity.class);
spec = tabHost.newTabSpec("个人中心").setIndicator(tab3).setContent(intent);
tabHost.addTab(spec);
intent = new Intent().setClass(this, LiveServiceActivity.class);
spec = tabHost.newTabSpec("生活服务").setIndicator(tab4).setContent(intent);
tabHost.addTab(spec);
intent = new Intent().setClass(this, appActivity.class);
spec = tabHost.newTabSpec("应用").setIndicator(tab5).setContent(intent);
tabHost.addTab(spec);
tabHost.setCurrentTab(mtabIndex);
gestureDetector = new GestureDetector(new MyGestureDetector());
gestureListener = new View.OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
if (gestureDetector.onTouchEvent(event)) {
return true;
}
return false;
}
};
}
// 手势监听器
class MyGestureDetector extends SimpleOnGestureListener {
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
float velocityY) {
TabHost tabHost = getTabHost();
try {
if (Math.abs(e1.getY() - e2.getY()) > SWIPE_MAX_OFF_PATH)
return false;
// right to left swipe
if (e1.getX() - e2.getX() > SWIPE_MIN_DISTANCE
&& Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
Log.i("test ", "right");
if (currentView == maxTabIndex) {
currentView = 0;
} else {
currentView++;
}
tabHost.setCurrentTab(currentView);
} else if (e2.getX() - e1.getX() > SWIPE_MIN_DISTANCE
&& Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
Log.i("test ", "left");
if (currentView == 0) {
currentView = maxTabIndex;
} else {
currentView--;
}
tabHost.setCurrentTab(currentView);
}
} catch (Exception e) {
// nothing
}
return false;
}
}
@Override
public boolean onTouchEvent(MotionEvent event) {
if (gestureDetector.onTouchEvent(event))
return true;
else
return false;
}
@Override
public boolean dispatchTouchEvent(MotionEvent event) {
if (gestureDetector.onTouchEvent(event)) {
event.setAction(MotionEvent.ACTION_CANCEL);
}
return super.dispatchTouchEvent(event);
}
}
-----------------------------------------------------------------------------------
main_tabwidget.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal" android:layout_width="fill_parent"
android:gravity="center"
android:layout_height="wrap_content">
<ImageButton android:id="@+id/tab1" android:layout_weight="0"
android:background="@drawable/main_tab_1_src" android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<ImageButton android:id="@+id/tab2" android:layout_weight="0"
android:background="@drawable/main_tab_2_src" android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<ImageButton android:id="@+id/tab3" android:layout_weight="0"
android:background="@drawable/main_tab_3_src" android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<ImageButton android:id="@+id/tab4" android:layout_weight="0"
android:background="@drawable/main_tab_4_src" android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<ImageButton android:layout_height="wrap_content" android:layout_width="wrap_content"
android:layout_weight="0" android:id="@+id/tab5"
android:background="@drawable/main_tab_5_src">
</ImageButton>
</LinearLayout>
------------------
main_tab_1_src.xml (该文件放在 drawable 文件夹下和main_tab_2_src.xml、main_tab_1_src.xml、main_tab_3_src.xml、main_tab_4_src.xml 一起)
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" android:drawable="@drawable/newsg" />
<item android:state_focused="true" android:state_selected="false"
android:drawable="@drawable/newsg" />
<item android:state_selected="true" android:drawable="@drawable/newsg" />
<item android:state_pressed="false" android:state_selected="false"
android:drawable="@drawable/news" />
</selector>
---------------------------------------------------------------------------------
//自定义自己Tabhost(
abhost 定议 :提供选项卡(Tab页)的窗口视图容器。此对象包含两个子对象:一组是用户可以选择指定Tab页的标签;另一组是FrameLayout用来显示该Tab页的内容。个别元素通常控制使用这个容器对象,而不是设置在子元素本身的值。)
package xxxxxxxxx.ui;
import android.content.Context;
import android.util.AttributeSet;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.TabHost;
import com.shoujifeng.cqlife.R;
/*
*
*/
public class MyTabHost extends TabHost {
private Animation slideLeftIn;
private Animation slideLeftOut;
private Animation slideRightIn;
private Animation slideRightOut;
private int tabCount;
public MyTabHost(Context context, AttributeSet attrs) {
super(context, attrs);
slideLeftIn = AnimationUtils.loadAnimation(context,R.anim.slide_left_in);
slideLeftOut = AnimationUtils.loadAnimation(context,R.anim.slide_left_out);
slideRightIn = AnimationUtils.loadAnimation(context,R.anim.slide_right_in);
slideRightOut = AnimationUtils.loadAnimation(context,R.anim.slide_right_out);
}
public int getTabCount() {
return tabCount;
}
@Override
public void addTab(TabSpec tabSpec) {
tabCount++;
super.addTab(tabSpec);
}
@Override
public void setCurrentTab(int index) {
//
int currentTabIndex = getCurrentTab();
//
if (null != getCurrentView()){//
if (currentTabIndex == (tabCount - 1) && index == 0) {//
getCurrentView().startAnimation(slideLeftOut);
} else if (currentTabIndex == 0 && index == (tabCount - 1)) {//
getCurrentView().startAnimation(slideRightOut);
} else if (index > currentTabIndex) {//
getCurrentView().startAnimation(slideLeftOut);
} else if (index < currentTabIndex) {//
getCurrentView().startAnimation(slideRightOut);
}
}
super.setCurrentTab(index);
//
if (currentTabIndex == (tabCount - 1) && index == 0){//
getCurrentView().startAnimation(slideLeftIn);
} else if (currentTabIndex == 0 && index == (tabCount - 1)) {//
getCurrentView().startAnimation(slideRightIn);
} else if (index > currentTabIndex) {//
getCurrentView().startAnimation(slideLeftIn);
} else if (index < currentTabIndex) {//
getCurrentView().startAnimation(slideRightIn);
}
}
}
-----------------------------------------
动画参数配置文件(放在res下的anim文件下 (新建一个anim文件夹))
slideLeftIn = AnimationUtils.loadAnimation(context,R.anim.slide_left_in);
slideLeftOut = AnimationUtils.loadAnimation(context,R.anim.slide_left_out);
slideRightIn = AnimationUtils.loadAnimation(context,R.anim.slide_right_in);
slideRightOut = AnimationUtils.loadAnimation(context,R.anim.slide_right_out);
slide_left_in.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate android:fromXDelta="100%p" android:toXDelta="0" android:duration="300"/>
</set>
slide_left_out.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate android:fromXDelta="0" android:toXDelta="-100%p" android:duration="300"/>
</set>
slide_right_in.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate android:fromXDelta="-100%p" android:toXDelta="0" android:duration="300"/>
</set>
slide_right_out.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate android:fromXDelta="0" android:toXDelta="100%p" android:duration="300"/>
</set>