- 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的例子了。
android tabHost添加滑动切换效果
最新推荐文章于 2016-02-18 15:50:47 发布