標籤:
實現了分頁的滑動效果,做的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底部菜單的迴圈滑動效果一