Android手势滑动Tab

//MainActivity.java
public class MainActivity 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;    private TabWidget tabWidget;    int currentView = 0;    private static int maxTabIndex = 2;    @Override    public void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.main);        TabHost tabHost = getTabHost();        tabHost.addTab(tabHost.newTabSpec("tab11").setIndicator("AAA ").setContent(new Intent(this, A.class)));        tabHost.addTab(tabHost.newTabSpec("tab12").setIndicator("BBB ").setContent(new Intent(this, B.class)));        tabHost.addTab(tabHost.newTabSpec("tab13").setIndicator("CCC ").setContent(new Intent(this, C.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;            }        };        tabWidget = tabHost.getTabWidget();        // 注意这个就是改变Tabhost默认样式的地方,一定将这部分代码放在上面这段代码的下面,不然样式改变不了        for (int i = 0; i < tabWidget.getChildCount(); i++) {            tabWidget.getChildAt(i).getLayoutParams().height = 70;// 设置选项卡的高度              tabWidget.getChildAt(i).getLayoutParams().width = 40;// 设置选项卡的宽度              TextView tv = (TextView) tabWidget.getChildAt(i).findViewById(android.R.id.title);            tv.setTextSize(15);            tv.setTextColor(this.getResources().getColorStateList(android.R.color.tertiary_text_dark));        }    }    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);    }}

//main.xml
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"    android:id="@android:id/tabhost"    android:layout_width="fill_parent"    android:layout_height="fill_parent" >    <LinearLayout        android:layout_width="fill_parent"        android:layout_height="fill_parent"        android:orientation="vertical" >        <FrameLayout            android:id="@android:id/tabcontent"            android:layout_width="fill_parent"            android:layout_height="1dip"            android:layout_weight="1" />        <TabWidget            android:id="@android:id/tabs"            android:layout_width="fill_parent"            android:layout_height="wrap_content"            android:paddingLeft="1dip"            android:paddingRight="1dip"            android:paddingTop="4dip" />    </LinearLayout></TabHost>

//slide_left_in.xml
<set xmlns:android="http://schemas.android.com/apk/res/android" >    <translate android:toXDelta="0"        android:duration="800"        android:fromXDelta="100%p" />   </set>
//slide_left_out.xml
<set xmlns:android="http://schemas.android.com/apk/res/android" >    <translate        android:duration="800"        android:fromXDelta="0"        android:toXDelta="-100%p" /></set>
//slide_right_in.xml
<set xmlns:android="http://schemas.android.com/apk/res/android" >    <translate        android:duration="800"        android:fromXDelta="-100%p"        android:toXDelta="0" /></set>
//slide_right_out.xml
<set xmlns:android="http://schemas.android.com/apk/res/android">    <translate android:fromXDelta="0"         android:toXDelta="100%p" android:duration="800"/></set>

参考链接:

代码下载链接:

更多相关文章

  1. android源代码下载——android环境配置
  2. Android EditText 代码实现键盘弹出打开和关闭工具类
  3. Android SDK 实例代码分析---Accelerometer Play(二)
  4. 如何通过代码更改ANDROID的UI布局
  5. 2010.11.28(2)———android 展示网页 和 调用js代码
  6. android 重用 c代码

随机推荐

  1. android根据屏幕高度改变item占ListView
  2. Activity 属性
  3. 关于打印日志的使用
  4. Android Intent和按钮响应事件的几种方式
  5. android实现开机自启动服务
  6. android4.3应用程序隐藏状态栏和标题栏
  7. Android 程序退出的办法
  8. 地图入门(一):Android上使用Google Maps加标
  9. android图形系统详解二:Drawables
  10. Android中的DownloadManager