Because the sliding effect of the page is used more throughout the application, all extracted into the parent class to introduce, see the code comment
Import Android.app.activity;import Android.content.sharedpreferences;import Android.os.bundle;import Android.view.gesturedetector;import Android.view.motionevent;import Android.view.view;import Android.view.gesturedetector.simpleongesturelistener;import android.widget.toast;//defines an abstract method Shownext, which is implemented by the base class as needed, Its attention function is sliding switch, you can follow 123 steps to learn public abstract class Basesetupactivity extends Activity {//1. Define a gesture recognizer private gesturedetector detector;protected sharedpreferences sp; @Overrideprotected void OnCreate (Bundle savedinstancestate) {//TODO Auto-generated method Stubsuper.oncreate (savedinstancestate); sp = getsharedpreferences ("config", mode_private);//2. Instantiating this gesture recognizer, Gesturedetector.simpleongesturelistener requires a separate reference detector = new Gesturedetector (This, new Simpleongesturelistener () {/** * When our fingers are sliding above the callback */@Overridepublic Boolean onfling (motionevent E1, motionevent E2, Float Velocityx, float velocityy) {//shielded at x sliding very slow case if (Math.Abs (Velocityx) <300) {Toast.maketext ( Getapplicationcontext (), "sliding too Slowly", 0). Show (); returntrue;} Shielded oblique Slip This case if (Math.Abs ((E2.getrawy ()-E1.getrawy ()) >) {toast.maketext (Getapplicationcontext (), "cannot be so slippery", 0). Show (); return true;} if ((E2.getrawx ()-e1.getrawx ()) > 200) {//Display previous page: Swipe from left to right System.out.println ("show previous page: Swipe from left to right"); Showpre (); return true;} if ((E1.getrawx ()-e2.getrawx ()) > 200) {//Show next page: Swipe from right to left System.out.println ("show next page: Swipe from right to left"); Shownext (); return true;} Return Super.onfling (E1, E2, Velocityx, Velocityy),}}); public abstract void Shownext ();p ublic abstract void Showpre ();/** * Next Click event * @param view */public void Next (view view) { Shownext ();} /** * Previous step * @param view */public void Pre (view view) {Showpre ();} 3. Using the gesture Recognizer @overridepublic Boolean ontouchevent (Motionevent event) {detector.ontouchevent (event); return Super.ontouchevent (event);}}
Swipe toggle and Extract parent class for Android screen page