Realize the sliding effect of the page, do the demo smooth operation note: Seemingly supported style (control) has a certain limit, I tried to text the ListView page, temporarily unable to achieve the sliding effect
Java files: Mainactivity.java, Activity1.java, Activity2.java, Activity3.java, Activity4.java
Mainactivity.java
PackageCom.example.tabhostmove;Importandroid.app.Activity;Importandroid.app.TabActivity;Importandroid.content.Intent;ImportAndroid.os.Bundle;ImportAndroid.util.Log;ImportAndroid.view.GestureDetector;ImportAndroid.view.Menu;ImportAndroid.view.MenuItem;Importandroid.view.MotionEvent;ImportAndroid.widget.TabHost;ImportAndroid.widget.TabHost.TabSpec; Public classMainactivityextendstabactivity {PrivateTabhost Tabhost; @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate); Setcontentview (R.layout.activity_main); Init (); } Private voidinit () {//TODO auto-generated Method StubTabhost=Gettabhost (); //Page 1Tabspec spec1 = Tabhost.newtabspec ("1"); Spec1.setindicator ("1", Getresources (). getdrawable (R.drawable.ic_launcher)); Intent Intent1=NewIntent ( This, Activity1.class); Spec1.setcontent (INTENT1); //Page 2Tabspec spec2 = Tabhost.newtabspec ("2"); Spec2.setindicator ("2", Getresources (). getdrawable (R.drawable.ic_launcher)); Intent Intent2=NewIntent ( This, Activity2.class); Spec2.setcontent (Intent2); //Page 3Tabspec SPEC3 = Tabhost.newtabspec ("3"); Spec3.setindicator ("3", Getresources (). getdrawable (R.drawable.ic_launcher)); Intent Intent3=NewIntent ( This, Activity3.class); Spec3.setcontent (INTENT3); //Page 4Tabspec SPEC4 = Tabhost.newtabspec ("4"); Spec4.setindicator ("4", Getresources (). getdrawable (R.drawable.ic_launcher)); Intent Intent4=NewIntent ( This, Activity4.class); Spec4.setcontent (INTENT4); Tabhost.addtab (SPEC1); Tabhost.addtab (SPEC2); Tabhost.addtab (SPEC3); Tabhost.addtab (SPEC4); } PrivateGesturedetector detector =NewGesturedetector (NewGesturedetector.simpleongesturelistener () {@Override Public BooleanOnfling (motionevent E1, motionevent E2,floatVelocityx,floatvelocityy) { 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 Booleanontouchevent (Motionevent event) {detector.ontouchevent (event); return Super. Ontouchevent (event); } /*** Current page index*/ inti = 0; /*** Show Next page*/ protected voidShownext () {//ternary expressions control the cycle of 3 pages. //Tabhost.setcurrenttab (i = i = = 3? i = 0: ++i); //log.i ("Kennet", I + ""); //Next loop for four pages Switch(i) { Case0: I++; Tabhost.setcurrenttab (i); Break; Case1: I++; Tabhost.setcurrenttab (i); Break; Case2: I++; Tabhost.setcurrenttab (i); Break; Case3: I=0; Tabhost.setcurrenttab (i); Break; } } /*** Show Previous page*/ protected voidShowpre () {//ternary expressions control the cycle of 3 pages. //Tabhost.setcurrenttab (i = i = = 0? i = 3:-I.); //four previous loops on a page Switch(i) { Case0: I=3; Tabhost.setcurrenttab (i); Break; Case1: I--; Tabhost.setcurrenttab (i); Break; Case2: I--; Tabhost.setcurrenttab (i); Break; Case3: I--; Tabhost.setcurrenttab (i); Break; }} @Override Public BooleanOncreateoptionsmenu (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 Booleanonoptionsitemselected (MenuItem item) {//Handle Action Bar item clicks here. The Action Bar would//automatically handle clicks on the Home/up button, so long//As you specify a the parent activity in Androidmanifest.xml. intID =Item.getitemid (); if(id = =r.id.action_settings) { return true; } return Super. onoptionsitemselected (item); }}
XML layout files: Activity_main.xml, Activit1.xml, Activit2.xml, Activit3.xml, Activit4.xml
Activity_main.xml
<Tabhostxmlns: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" > <LinearLayoutAndroid:layout_width= "Fill_parent"Android:layout_height= "Fill_parent"android:orientation= "vertical" > <TabwidgetAndroid:id= "@android: Id/tabs"Android:layout_width= "Fill_parent"Android:layout_height= "Wrap_content" /> <FramelayoutAndroid:id= "@android: Id/tabcontent"Android:layout_width= "Fill_parent"Android:layout_height= "Wrap_content"Android:layout_weight= "1" > </Framelayout> </LinearLayout></Tabhost>
Note: activity1, 2, 3, 4 is the test page, just build a few can, do not forget the registration page in the Androidmanifest.xml activity
Implementation results:
The loop slide effect of the top or bottom menu of the Android UI