【Android UI】頂部or底部菜單的迴圈滑動效果一

來源:互聯網
上載者:User

標籤:

實現了分頁的滑動效果,做的demo流暢運行註:貌似支援的樣式(控制項)有一定的限制,我試過簡訊的listview頁面,暫無法實現滑動效果

java檔案:MainActivity.java、Activity1.java、Activity2.java、Activity3.java、Activity4.java

MainActivity.java

package com.example.tabhostmove;import android.app.Activity;import android.app.TabActivity;import android.content.Intent;import android.os.Bundle;import android.util.Log;import android.view.GestureDetector;import android.view.Menu;import android.view.MenuItem;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() {        // TODO Auto-generated method stub                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);        // 頁面3        TabSpec spec3 = tabHost.newTabSpec("3");        spec3.setIndicator("3", getResources().getDrawable(R.drawable.ic_launcher));        Intent intent3 = new Intent(this, Activity3.class);        spec3.setContent(intent3);                // 頁面4        TabSpec spec4 = tabHost.newTabSpec("4");        spec4.setIndicator("4", getResources().getDrawable(R.drawable.ic_launcher));        Intent intent4 = new Intent(this, Activity4.class);        spec4.setContent(intent4);        tabHost.addTab(spec1);        tabHost.addTab(spec2);        tabHost.addTab(spec3);        tabHost.addTab(spec4);            }    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 == 3 ? i = 0 : ++i);        //Log.i("kennet", i + "");        //四個頁面的下一個迴圈        switch(i)        {        case 0:            i++;            tabHost.setCurrentTab(i);            break;        case 1:            i++;            tabHost.setCurrentTab(i);            break;        case 2:            i++;            tabHost.setCurrentTab(i);            break;        case 3:            i=0;            tabHost.setCurrentTab(i);            break;        }    }    /**     * 顯示前一個頁面     */    protected void showPre() {        // 三元運算式控制3個頁面的迴圈.        //tabHost.setCurrentTab(i = i == 0 ? i = 3 : --i);        //四個頁面的上一個迴圈        switch(i)        {        case 0:            i=3;            tabHost.setCurrentTab(i);            break;        case 1:            i--;            tabHost.setCurrentTab(i);            break;        case 2:            i--;            tabHost.setCurrentTab(i);            break;        case 3:            i--;            tabHost.setCurrentTab(i);            break;        }    }    @Override    public boolean onCreateOptionsMenu(Menu menu) {        // Inflate the menu; this adds items to the action bar if it is present.        getMenuInflater().inflate(R.menu.main, menu);        return true;    }    @Override    public boolean onOptionsItemSelected(MenuItem item) {        // Handle action bar item clicks here. The action bar will        // automatically handle clicks on the Home/Up button, so long        // as you specify a parent activity in AndroidManifest.xml.        int id = item.getItemId();        if (id == R.id.action_settings) {            return true;        }        return super.onOptionsItemSelected(item);    }}

xml布局檔案:activity_main.xml、activit1.xml、activit2.xml、activit3.xml、activit4.xml

activity_main.xml

<TabHost xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:id="@android:id/tabhost"    android:layout_width="match_parent"    android:layout_height="match_parent" >    <LinearLayout        android:layout_width="fill_parent"        android:layout_height="fill_parent"        android:orientation="vertical" >        <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="wrap_content"            android:layout_weight="1" >        </FrameLayout>    </LinearLayout></TabHost>

註:activity1、2、3、4是測試的頁面,隨便建幾個即可,別忘了在AndroidManifest.xml裡註冊頁面的活動

實現效果:

【Android UI】頂部or底部菜單的迴圈滑動效果一

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.