tabhost通过手势滑动切换activity

  1. package com.mars.mp3player;  
  2.   
  3. import android.app.TabActivity;  
  4. import android.content.Intent;  
  5. import android.os.Bundle;  
  6. import android.util.Log;  
  7. import android.view.GestureDetector;  
  8. import android.view.GestureDetector.SimpleOnGestureListener;  
  9. import android.view.Menu;  
  10. import android.view.MotionEvent;  
  11. import android.view.View;  
  12. import android.widget.TabHost;  
  13. import android.widget.TabHost.TabSpec;  
  14.   
  15. public class ActivityMain extends TabActivity {  
  16.     private TabSpec tabSpec;  
  17.     private TabHost tabHost;  
  18.     private Intent intent;  
  19.     // *********************************  
  20.     private static final int SWIPE_MIN_DISTANCE = 120;  
  21.     private static final int SWIPE_MAX_OFF_PATH = 250;  
  22.     private static final int SWIPE_THRESHOLD_VELOCITY = 200;  
  23.     private GestureDetector gestureDetector;  
  24.     View.OnTouchListener gestureListener;  
  25.   
  26.     int currentView = 0;  
  27.     private static int maxTabIndex = 2;  
  28.   
  29.     @Override  
  30.     public void onCreate(Bundle savedInstanceState) {  
  31.         super.onCreate(savedInstanceState);  
  32.         super.setContentView(R.layout.activity_main);  
  33.         UtilVar.activities.add(ActivityMain.this);  
  34.         this.initTableHost();  
  35.   
  36.         gestureDetector = new GestureDetector(new MyGestureDetector());  
  37.         gestureListener = new View.OnTouchListener() {  
  38.             public boolean onTouch(View v, MotionEvent event) {  
  39.                 if (gestureDetector.onTouchEvent(event)) {  
  40.                     return true;  
  41.                 }  
  42.                 return false;  
  43.             }  
  44.         };  
  45.   
  46.     }  
  47.   
  48.     /** 
  49.      * tablehost的初始化,代码完成 
  50.      */  
  51.     private void initTableHost() {  
  52.         tabHost = super.getTabHost();  
  53.         // 歌曲列表  
  54.         intent = new Intent(ActivityMain.this, ActivityList.class);  
  55.         tabSpec = tabHost  
  56.                 .newTabSpec("歌曲列表")  
  57.                 .setIndicator("歌曲列表",  
  58.                         getResources().getDrawable(R.drawable.ic_launcher))  
  59.                 .setContent(intent);  
  60.         tabHost.addTab(tabSpec);  
  61.         // 正在播放  
  62.         intent = new Intent(ActivityMain.this, ActivityPlaying.class);  
  63.         tabSpec = tabHost  
  64.                 .newTabSpec("正在播放")  
  65.                 .setIndicator("正在播放",  
  66.                         getResources().getDrawable(R.drawable.ic_launcher))  
  67.                 .setContent(intent);  
  68.         tabHost.addTab(tabSpec);  
  69.         // 专辑列表  
  70.         intent = new Intent(ActivityMain.this, ActivityLyric.class);  
  71.         tabSpec = tabHost  
  72.                 .newTabSpec("歌词显示")  
  73.                 .setIndicator("歌词显示",  
  74.                         getResources().getDrawable(R.drawable.ic_launcher))  
  75.                 .setContent(intent);  
  76.         tabHost.addTab(tabSpec);  
  77.         // 设置当前tab  
  78.         tabHost.setCurrentTab(1);  
  79.     }  
  80.   
  81.     // 左右滑动刚好页面也有滑动效果  
  82.     class MyGestureDetector extends SimpleOnGestureListener {  
  83.         @Override  
  84.         public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,  
  85.                 float velocityY) {  
  86.             TabHost tabHost = getTabHost();  
  87.             System.out.println("************");  
  88.             try {  
  89.                 if (Math.abs(e1.getY() - e2.getY()) > SWIPE_MAX_OFF_PATH)  
  90.                     return false;  
  91.                 // right to left swipe  
  92.                 if (e1.getX() - e2.getX() > SWIPE_MIN_DISTANCE  
  93.                         && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {  
  94.                     Log.i("test""right");  
  95.                     if (currentView == maxTabIndex) {  
  96.                         currentView = 0;  
  97.                     } else {  
  98.                         currentView++;  
  99.                     }  
  100.                     tabHost.setCurrentTab(currentView);  
  101.                 } else if (e2.getX() - e1.getX() > SWIPE_MIN_DISTANCE  
  102.                         && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {  
  103.                     Log.i("test""left");  
  104.                     if (currentView == 0) {  
  105.                         currentView = maxTabIndex;  
  106.                     } else {  
  107.                         currentView--;  
  108.                     }  
  109.                     tabHost.setCurrentTab(currentView);  
  110.                 }  
  111.             } catch (Exception e) {  
  112.             }  
  113.             return false;  
  114.         }  
  115.     }  
  116.   
  117.     @Override  
  118.     public boolean dispatchTouchEvent(MotionEvent event) {  
  119.         if (gestureDetector.onTouchEvent(event)) {  
  120.             event.setAction(MotionEvent.ACTION_CANCEL);  
  121.         }  
  122.         return super.dispatchTouchEvent(event);  
  123.     }  
  124.   
  125.     @Override  
  126.     public boolean onCreateOptionsMenu(Menu menu) {  
  127.         getMenuInflater().inflate(R.menu.activity_main, menu);  
  128.         return true;  
  129.     }  
  130.   
  131. }  
  132. // 主要代码就这样吧,以后就不必到处找手势跳转activity的例子了。  

你可能感兴趣的:(tabhost通过手势滑动切换activity)