tabhost通过手势滑动切换activity


package com.mars.mp3player;

import android.app.TabActivity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.GestureDetector;
import android.view.GestureDetector.SimpleOnGestureListener;
import android.view.Menu;
import android.view.MotionEvent;
import android.view.View;
import android.widget.TabHost;
import android.widget.TabHost.TabSpec;

public class ActivityMain extends TabActivity {
private TabSpec tabSpec;
private TabHost tabHost;
private Intent intent;
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;

int currentView = 0;
private static int maxTabIndex = 2;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
super.setContentView(R.layout.activity_main);
UtilVar.activities.add(ActivityMain.this);
this.initTableHost();

gestureDetector = new GestureDetector(new MyGestureDetector());
gestureListener = new View.OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
if (gestureDetector.onTouchEvent(event)) {
return true;
}
return false;
}
};

}

/**
* tablehost的初始化,代码完成
*/
private void initTableHost() {
tabHost = super.getTabHost();
// 歌曲列表
intent = new Intent(ActivityMain.this, ActivityList.class);
tabSpec = tabHost
.newTabSpec("歌曲列表")
.setIndicator("歌曲列表",
getResources().getDrawable(R.drawable.ic_launcher))
.setContent(intent);
tabHost.addTab(tabSpec);
// 正在播放
intent = new Intent(ActivityMain.this, ActivityPlaying.class);
tabSpec = tabHost
.newTabSpec("正在播放")
.setIndicator("正在播放",
getResources().getDrawable(R.drawable.ic_launcher))
.setContent(intent);
tabHost.addTab(tabSpec);
// 专辑列表
intent = new Intent(ActivityMain.this, ActivityLyric.class);
tabSpec = tabHost
.newTabSpec("歌词显示")
.setIndicator("歌词显示",
getResources().getDrawable(R.drawable.ic_launcher))
.setContent(intent);
tabHost.addTab(tabSpec);
// 设置当前tab
tabHost.setCurrentTab(1);
}

// 左右滑动刚好页面也有滑动效果
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) {
}
return false;
}
}

@Override
public boolean dispatchTouchEvent(MotionEvent event) {
if (gestureDetector.onTouchEvent(event)) {
event.setAction(MotionEvent.ACTION_CANCEL);
}
return super.dispatchTouchEvent(event);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}

}
// 主要代码就这样吧,以后就不必到处找手势跳转activity的例子了。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值