Previously with Tabhost just click on the Navigation bar tab to switch, today tried the next gesture swipe to switch, searched a lot of information feel particularly chaotic, spent a good long time finishing a bit finally has the effect, wrote a demo.
Program Listing 1: Layout file
Description: Same as we write Tabhost layout file
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= "Hello"
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= "Hello"
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= "Hello"
Android:textsize= "20sp"/>
<listview
Android:id= "@+id/listview2"
Android:layout_width= "Match_parent"
android:layout_height= "360DP"
>
</ListView>
</LinearLayout>
</FrameLayout>
</LinearLayout>
</TabHost>
</LinearLayout>
Program Listing 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);
}
}
Of course, there are four XML files about sliding that we'll be using in Res\anim (anim file to create a new one)
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>
Well, it's done now. Here are a few:
This article is from my blog blog, so be sure to keep this source http://huolailai.blog.51cto.com/9946966/1656387
Android Development--Realize tabhost sliding switch tab function (absolutely practical)