TabHost多界面手勢滑動切換

import android.app.TabActivity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.widget.TabHost;
import android.widget.TabHost.TabSpec;
 
public class MainActivity extends TabActivity {
        private TabHost tabHost;
 
        @Override
        protected void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.activity_main);
                init();
        }
 
        private void init() {
                tabHost = getTabHost();
                // 頁面1
                TabSpec spec1 = tabHost.newTabSpec("1");
                spec1.setIndicator("1", getResources().getDrawable(R.drawable.ic_launcher));
                Intent intent1 = new Intent(this, Activity1.class);
                spec1.setContent(intent1);
 
                // 頁面2
                TabSpec spec2 = tabHost.newTabSpec("2");
                spec2.setIndicator("2", getResources().getDrawable(R.drawable.ic_launcher));
                Intent intent2 = new Intent(this, Activity2.class);
                spec2.setContent(intent2);
 
                // 頁面1
                TabSpec spec3 = tabHost.newTabSpec("3");
                spec3.setIndicator("3", getResources().getDrawable(R.drawable.ic_launcher));
                Intent intent3 = new Intent(this, Activity3.class);
                spec3.setContent(intent3);
 
                tabHost.addTab(spec1);
                tabHost.addTab(spec2);
                tabHost.addTab(spec3);
 
        }
 
        private GestureDetector detector = new GestureDetector(new GestureDetector.SimpleOnGestureListener() {
 
                @Override
                public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
                        if ((e2.getRawX() - e1.getRawX()) > 80) {
                                showNext();
                                return true;
                        }
 
                        if ((e1.getRawX() - e2.getRawX()) > 80) {
                                showPre();
                                return true;
                        }
                        return super.onFling(e1, e2, velocityX, velocityY);
                }
 
        });
 
        @Override
        public boolean onTouchEvent(MotionEvent event) {
                detector.onTouchEvent(event);
                return super.onTouchEvent(event);
        }
 
        /**
         * 當前頁面索引
         */
        int i = 0;
 
        /**
         * 顯示下一個頁面
         */
        protected void showNext() {
                // 三元表達式控制3個頁面的循環.
                tabHost.setCurrentTab(i = i == 2 ? i = 0 : ++i);
                Log.i("kennet", i + "");
 
        }
 
        /**
         * 顯示前一個頁面
         */
        protected void showPre() {
                // 三元表達式控制3個頁面的循環.
                tabHost.setCurrentTab(i = i == 0 ? i = 2 : --i);
 
        }
 
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章