Android手势滑动Tab

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>

 

参考链接:

代码下载链接:

你可能感兴趣的:(android)