欧美一区二区三区老妇人-欧美做爰猛烈大尺度电-99久久夜色精品国产亚洲a-亚洲福利视频一区二区

Android開(kāi)發(fā)——實(shí)現(xiàn)TabHost隨手滑動(dòng)切換選項(xiàng)卡功能(絕對(duì)實(shí)用)

    以前用TabHost只是點(diǎn)擊導(dǎo)航欄選項(xiàng)卡才進(jìn)行切換,今天試了下手勢(shì)滑動(dòng)進(jìn)行切換,搜了好多資料感覺(jué)特別亂,花了好長(zhǎng)時(shí)間整理了一下終于有效果了,自己寫(xiě)了一個(gè)Demo。

成都創(chuàng)新互聯(lián)公司長(zhǎng)期為成百上千客戶提供的網(wǎng)站建設(shè)服務(wù),團(tuán)隊(duì)從業(yè)經(jīng)驗(yàn)10年,關(guān)注不同地域、不同群體,并針對(duì)不同對(duì)象提供差異化的產(chǎn)品和服務(wù);打造開(kāi)放共贏平臺(tái),與合作伙伴共同營(yíng)造健康的互聯(lián)網(wǎng)生態(tài)環(huán)境。為隆林企業(yè)提供專業(yè)的網(wǎng)站設(shè)計(jì)制作、成都網(wǎng)站建設(shè)隆林網(wǎng)站改版等技術(shù)服務(wù)。擁有十年豐富建站經(jīng)驗(yàn)和眾多成功案例,為您定制開(kāi)發(fā)。

    程序清單1:布局文件

        說(shuō)明:和我們寫(xiě)Tabhost布局文件一樣

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout  xmlns:android="http://schemas.android.com/apk/res/android"

    android:orientation="vertical"

    android:layout_width="fill_parent"

    android:layout_height="fill_parent">

    <TabHost

        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">

            <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" >

                <LinearLayout

                    android:focusable="true"

                    android:focusableInTouchMode="true"

                    android:id="@+id/tab01"

                    android:orientation="vertical"

                    android:layout_width="fill_parent"

                    android:layout_height="fill_parent">

                    <TextView

                        android:layout_height="wrap_content"

                        android:layout_width="wrap_content"

                        android:text="你好"

                        android:textSize="20sp"/>

                    <ListView

                        android:id="@+id/listview"

                        android:layout_width="match_parent"

                        android:layout_height="match_parent"

                        android:dividerHeight="10dp"

                        android:divider="#D1D1D1"

                        >

                    </ListView>

                </LinearLayout>

                <LinearLayout

                    android:id="@+id/tab02"

                    android:orientation="vertical"

                    android:layout_width="fill_parent"

                    android:layout_height="fill_parent">

                    <TextView

                        android:layout_height="wrap_content"

                        android:layout_width="wrap_content"

                        android:text="你好"

                        android:textSize="20sp"/>

                    <ListView

                        android:id="@+id/listview1"

                        android:layout_width="match_parent"

                        android:layout_height="match_parent">

                    </ListView>

                </LinearLayout>

                <LinearLayout

                    android:id="@+id/tab03"

                    android:orientation="vertical"

                    android:layout_width="fill_parent"

                    android:layout_height="fill_parent">

                    <TextView

                        android:layout_height="wrap_content"

                        android:layout_width="wrap_content"

                        android:text="你好"

                        android:textSize="20sp"/>

                    <ListView

                        android:id="@+id/listview2"

                        android:layout_width="match_parent"

                        android:layout_height="360dp"

                        >

                    </ListView>

                </LinearLayout>

                </FrameLayout>

        </LinearLayout>

    </TabHost>

</LinearLayout>

    

程序清單2:

 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;

    int currentView = 0;

    private static int maxTabIndex = 2;

    /** Called when the activity is first created. */

    @Override

    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);

        TabHost tabHost = getTabHost();

        tabHost.addTab(tabHost.newTabSpec("tab1").setIndicator("tab1 ")

                .setContent(R.id.tab01));

        tabHost.addTab(tabHost.newTabSpec("tab2").setIndicator("tab2 ")

                .setContent(R.id.tab02));

        tabHost.addTab(tabHost.newTabSpec("tab3").setIndicator("tab3 ")

                .setContent(R.id.tab03));

        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;

            }

        };

    }

    class MyGestureDetector extends GestureDetector.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;

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

// nothing

            }

            return false;

        }

    }

//@Override

//public boolean onTouchEvent(MotionEvent event) {

//if (gestureDetector.onTouchEvent(event))

//return true;

//else

//return false;

//}

    @Override

    public boolean dispatchTouchEvent(MotionEvent event) {

        if(gestureDetector.onTouchEvent(event)){

            event.setAction(MotionEvent.ACTION_CANCEL);

        }

        return super.dispatchTouchEvent(event);

    }

}

當(dāng)然這里會(huì)用到關(guān)于滑動(dòng)的四個(gè)xml文件   我們將它們存在 res\anim中 (anim文件要自己新建)

1、 slide_left_in

2、slide_lefe_out

3、slide_right_in

4、slide_right_out

1、 slide_left_in.xml

<?xml version="1.0" encoding="utf-8"?>

<set xmlns:android="http://schemas.android.com/apk/res/android" >

    <translate

        android:duration="500"

        android:fromXDelta="100%p"

        android:toXDelta="0" />

    <alpha

        android:duration="500"

        android:fromAlpha="0.0"

        android:toAlpha="1.0" />

</set>

2、slide_lefe_out.xml

<?xml version="1.0" encoding="utf-8"?>

<set xmlns:android="http://schemas.android.com/apk/res/android" >

    <translate

        android:duration="500"

        android:fromXDelta="0"

        android:toXDelta="-100%p" />

    <alpha

        android:duration="500"

        android:fromAlpha="1.0"

        android:toAlpha="0.0" />

</set>

 3、slide_right_in.xml

<?xml version="1.0" encoding="utf-8"?>

<set xmlns:android="http://schemas.android.com/apk/res/android" >

    <translate

        android:duration="500"

        android:fromXDelta="-100%p"

        android:toXDelta="0" />

    <alpha

        android:duration="500"

        android:fromAlpha="0.0"

        android:toAlpha="1.0" />

</set>

 4、slide_right_out.xml

<?xml version="1.0" encoding="utf-8"?>

<set xmlns:android="http://schemas.android.com/apk/res/android" >

    <translate

        android:duration="1500"

        android:fromXDelta="0"

        android:toXDelta="100%p" />

    <alpha

        android:duration="1500"

        android:fromAlpha="1.0"

        android:toAlpha="0.1" />

</set>

好了現(xiàn)在就完成了。來(lái)幾張效果圖:

Android開(kāi)發(fā)——實(shí)現(xiàn)TabHost 隨手滑動(dòng)切換選項(xiàng)卡功能(絕對(duì)實(shí)用)    Android開(kāi)發(fā)——實(shí)現(xiàn)TabHost 隨手滑動(dòng)切換選項(xiàng)卡功能(絕對(duì)實(shí)用)      Android開(kāi)發(fā)——實(shí)現(xiàn)TabHost 隨手滑動(dòng)切換選項(xiàng)卡功能(絕對(duì)實(shí)用)

附件:http://down.51cto.com/data/2365745

文章標(biāo)題:Android開(kāi)發(fā)——實(shí)現(xiàn)TabHost隨手滑動(dòng)切換選項(xiàng)卡功能(絕對(duì)實(shí)用)
分享網(wǎng)址:http://chinadenli.net/article48/jgjjhp.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供小程序開(kāi)發(fā)用戶體驗(yàn)搜索引擎優(yōu)化服務(wù)器托管手機(jī)網(wǎng)站建設(shè)App設(shè)計(jì)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)

成都做網(wǎng)站