Tabhost页面实现左右滑动进行页面切换

TabHost页面滑动切换
本文介绍了一种使用TabHost实现页面左右滑动切换的方法。通过重写onTouchEvent方法并结合GestureDetector,实现了根据用户的滑动手势来切换不同的Tab页面。示例中包含了三个页面的循环切换,并提供了完整的MainActivity代码。

activity_main.xml布局文件:

<?xml version="1.0" encoding="utf-8"?>
<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:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:padding="5dp">
        <TabWidget
            android:id="@android:id/tabs"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content" />
        <FrameLayout
            android:id="@android:id/tabcontent"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:padding="5dp" />
    </LinearLayout>
</TabHost>
Tabhost页面实现左右滑动进行页面切换
http://www.eoeandroid.com/thread-247700-1-1.html

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);

        }

}


评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值