[color=red]从右向左滑动,tab页切换后的效果[/color]
[img]http://dl.iteye.com/upload/attachment/561291/46bb2c05-2650-3877-9175-447de4e15fc0.png[/img]
[img]http://dl.iteye.com/upload/attachment/561293/b397b387-50dc-30bd-b963-20e79747ba00.png[/img]
[color=red]主要代码1:继承TabHost覆写setCurrentTab(int index)方法[/color]
[color=red]2:实现OnGestureListener接口,覆写onFling()方法[/color]
[img]http://dl.iteye.com/upload/attachment/561291/46bb2c05-2650-3877-9175-447de4e15fc0.png[/img]
[img]http://dl.iteye.com/upload/attachment/561293/b397b387-50dc-30bd-b963-20e79747ba00.png[/img]
[color=red]主要代码1:继承TabHost覆写setCurrentTab(int index)方法[/color]
@Override
public void setCurrentTab(int index) {
//index为要切换到的tab页索引,currentTabIndex为现在要当前tab页的索引
int currentTabIndex = getCurrentTab();
//设置当前tab页退出时的动画
if (null != getCurrentView()){//第一次进入MainActivity时,getCurrentView()取得的值为空
if (currentTabIndex == (tabCount - 1) && index == 0) {//处理边界滑动
getCurrentView().startAnimation(slideLeftOut);
} else if (currentTabIndex == 0 && index == (tabCount - 1)) {//处理边界滑动
getCurrentView().startAnimation(slideRightOut);
} else if (index > currentTabIndex) {//非边界情况下从右往左fleep
getCurrentView().startAnimation(slideLeftOut);
} else if (index < currentTabIndex) {//非边界情况下从左往右fleep
getCurrentView().startAnimation(slideRightOut);
}
}
super.setCurrentTab(index);
//设置即将显示的tab页的动画
if (currentTabIndex == (tabCount - 1) && index == 0){//处理边界滑动
getCurrentView().startAnimation(slideLeftIn);
} else if (currentTabIndex == 0 && index == (tabCount - 1)) {//处理边界滑动
getCurrentView().startAnimation(slideRightIn);
} else if (index > currentTabIndex) {//非边界情况下从右往左fleep
getCurrentView().startAnimation(slideLeftIn);
} else if (index < currentTabIndex) {//非边界情况下从左往右fleep
getCurrentView().startAnimation(slideRightIn);
}
}
[color=red]2:实现OnGestureListener接口,覆写onFling()方法[/color]
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,float velocityY) {
if (e1.getX() - e2.getX() <= (-FLEEP_DISTANCE)) {//从左向右滑动
currentTabID = tabHost.getCurrentTab() - 1;
if (currentTabID < 0) {
currentTabID = tabHost.getTabCount() - 1;
}
} else if (e1.getX() - e2.getX() >= FLEEP_DISTANCE) {//从右向左滑动
currentTabID = tabHost.getCurrentTab() + 1;
if (currentTabID >= tabHost.getTabCount()) {
currentTabID = 0;
}
}
tabHost.setCurrentTab(currentTabID);
return false;
}