Swipe toggle and Extract parent class for Android screen page

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.