public class Main extends TabActivity {
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 Animation slideLeftIn;
private Animation slideLeftOut;
private Animation slideRightIn;
private Animation slideRightOut;
private ViewFlipper viewFlipper;
int currentView = 0;
private static int maxTabIndex = 2;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
TabHost tabHost = getTabHost();
tabHost.addTab(tabHost.newTabSpec("tab1").setIndicator("tab1 ")
.setContent(new Intent(this, Activity1.class)));
tabHost.addTab(tabHost.newTabSpec("tab2").setIndicator("tab2 ")
.setContent(new Intent(this, Activity2.class)));
tabHost.addTab(tabHost.newTabSpec("tab3").setIndicator("tab3 ")
.setContent(new Intent(this, Activity3.class)));
tabHost.setCurrentTab(0);
slideLeftIn = AnimationUtils.loadAnimation(this, R.anim.slide_left_in);
slideLeftOut = AnimationUtils
.loadAnimation(this, R.anim.slide_left_out);
slideRightIn = AnimationUtils
.loadAnimation(this, R.anim.slide_right_in);
slideRightOut = AnimationUtils.loadAnimation(this,
R.anim.slide_right_out);
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);
}
}
xml文件如下:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<ImageButton
android:id="@+id/button1"
android:layout_weight="1"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</ImageButton>
<ImageButton
android:id="@+id/button2"
android:layout_weight="1"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</ImageButton>
</LinearLayout>
<TabHost
android:id="@android:id/tabhost" android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout android:orientation="vertical"
android:layout_width="fill_parent" android:layout_height="fill_parent">
<TabWidget android:id="@android:id/tabs"
android:layout_width="fill_parent" android:layout_height="wrap_content" />
<FrameLayout android:id="@android:id/tabcontent"
android:layout_width="fill_parent" android:layout_height="fill_parent" />
</LinearLayout>
</TabHost>
</LinearLayout>
分享到:
相关推荐
tabhost+view结合使用的demo,手势滑动、选项卡的同时实现,代码清晰,注解详细,因为太懒,用的是sdk自带的那个图片,大家到时候直接换掉图片就ok了^_^
简单的例子 能够运行流畅 是想手机一样的 通过手指左右滑动切换tabhost
继承TabActivity实现底部导航功能,自定义导航栏样式,可以滑动切换选项卡
Android可以点击切换和滑动切换的tabhost
自定义TabHost放在Activity底部,实现多个Activity的切换!
1.利用tabHost加ViewPager实现滑动的切换选项卡 2.有详细的注释,可自行修改界面风格 3.项目基于2.2开发,可兼容所有手机
完整的实现了通过手势控制tabhost里面活动的切换 对于刚刚学习android的人来说可以算是不错的资源 欢迎交流
解决tabhost子activity不响应OnActivityResult()函数
TabHost内实现多个Activity的跳转
1:解决了tabhost 位于底部的问题; 2:解决了tabhost 一个tab页中多个activity的跳转及返回问题。
项目需求,需要做一个TabHost,而且菜单是在下栏的,并且实现左右滑动切换Activity,而且下栏跟着改变到对应的栏目的,我便做了一个demo,这个demo的功能很完美,固上传上来给有需要的朋友。
TabHost+ViewPager实现滑动tabhost, viewPager;tabhost;滑动;源码
自定义Android中TabHost组件显示在屏幕底部,并实现滑动切换页面。在此基础上可以实现类似米聊等软件主界面的Tab效果。
选项卡切换效果,内容滑动切换。两个简单小例子仅供大家学习参考用。
简单的android选项卡Tab菜单TabHost(一)同一Activity上实现
TabHostActivity在API 13之后就不能用了。 写了个Demo,用FragmentActivity + Tabhost实现多标签页,并添加滑动手势。 API=19,可运行
在Android下实现的tabhost的滑动翻页的效果。
android实现tabhost并且tab内容是listview,没有view可以左右滑动。
利用Tabhost + Fragment实现android标签切换界面.
可以滑动的TabHost 基于ViewPaget实现 可能需要导入包,在libs中