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(安卓)改变AlertDialog的大小
  2. android ndk 入门实践
  3. SD卡读写文件 代码学习
  4. android源代码下载——android环境配置
  5. android 中限制EditText 最大输入字符数
  6. Android(安卓)EditText 代码实现键盘弹出打开和关闭工具类
  7. Android(安卓)SDK 实例代码分析---Accelerometer Play(二)
  8. android-单元测试(Android(安卓)JUnit Test)
  9. 在Android上执行Java程序

随机推荐

  1. Android(安卓)MediaExtractor setDataSou
  2. Caused by: org.gradle.api.internal.plu
  3. Android(安卓)service通信
  4. Android中的ClassLoader
  5. android 下测试网络的丢包率和延迟 的代
  6. androidd 几个开源框架
  7. Android(安卓)ViewPager2+Fragment+TabLa
  8. Android开机自启动
  9. Android中的classLoader
  10. android中的自定义控件