Android instance-Mobile security Defender (23)-Custom abstract class and use

Source: Internet
Author: User

First, the goal.

Extract the code from section 22 to toggle Activity interface effects into a custom abstract class and define an abstract method that facilitates the invocation of other classes. That is, other activity, such as Setup Wizards 2, 3, 4, can quickly implement sliding switching by inheriting a custom class and implementing its non-implemented methods.

Second, the code implementation.

1. Create a new class under the package (named Slideactivity), inherit activity, and specify its type as abstract (public abstract class Slideactivity extends Activity). In the custom abstract class code, define two abstract methods (publicabstract void), named Slidenext () and Slidepre (), respectively, Indicates slide to next and slide up one step. The two abstract methods allow you to inherit the subclass of this new class. The new class is now in order to determine when to invoke these two abstract methods based on the result of the sliding direction.

2, define a gesture recognizer (gesturedetector) object (named detector), the OnCreate method of replication. (The following steps are similar to section 22, except that the IF statement executes a different abstract method according to the judging criteria) in the OnCreate method (new Gesturedetector (context, listener)), the parameter context is Listener is the listener. The listener is instantiated through new, and the object of New is Simpleongesturelistener () {...}. In the Listener:

①. Onfling (motionevent E1, motionevent E2, float Velocityx, float velocityy), parameter motionevent E1 indicates the event that the finger falls to the screen, Motionevent E2 represents movement events caused by the current action, float Velocityx, float velocityy, respectively, represent the movement speed of the current action on the x-axis and the y-axis.

②. Gets the x-axis coordinate point (E2.GETRAWX (), E1.GETRAWX ()) where the finger falls to the event on the screen and moves the event, via if () ... Determine the direction of the slide by judging the difference between the two coordinate points. When the x-axis coordinates of the E2 are larger than the x-axis coordinates of E1, the gesture is left-to-right, and a method that slides up one step (Slidepre ()) is called, and when E1 's x-axis coordinates are greater than the x-coordinate of the E2, the gesture is left-right, and the call slips to the next ()) method. It is also possible to judge by their difference greater than a value (such as 100) that the statement inside the IF is executed only if the distance of the slide exceeds the value.

③. Because the return value type of the Onfling method is Boolean, you need to return true at the end of the IF statement. The last return statement of the method does not change.

3, in the new abstract class code in the replication Touch event method Ontouchevent (Motionevent event), the parameter Motionevent event is the current touch events. In this method, the current touch event is passed in by means of the ontouchevent (motionevent ev) method of the gesture recognizer (Gesturedetector) object (detector).

4, because in the previous production "previous", "Next" button style, the Click event (pre (view), next (view) is placed in the style, so you can also be the two click events in the new abstract class (slideactivity) implementation, The method is to perform a slip-up step (Slidepre ()) and slide the statement to the next (Slidenext ()) method respectively.

Code for the new abstract class (slideactivity):

1  PackageCom.example.mobilesafe;2 3 Importandroid.app.Activity;4 ImportAndroid.view.GestureDetector;5 ImportAndroid.view.GestureDetector.SimpleOnGestureListener;6 Importandroid.view.MotionEvent;7 ImportAndroid.view.View;8 9  Public Abstract classSlideactivityextendsActivity {Ten  One      Public Abstract voidSlidenext (); A  -      Public Abstract voidSlidepre (); -  the     //1. Define gesture recognizer -     PrivateGesturedetector detector; -  - @Override +     protected voidonCreate (Android.os.Bundle savedinstancestate) { -         Super. OnCreate (savedinstancestate); +detector =NewGesturedetector ( This,NewSimpleongesturelistener () { A  at @Override -              Public Booleanonfling (motionevent E1, motionevent E2, -                     floatVelocityx,floatvelocityy) { -                 if((E2.GETRAWX ()-e1.getrawx ()) > 200) { -                     //swipe from left to right, slide to previous page - Slidepre (); in                     return true; -                 } to                 if((E1.GETRAWX ()-e2.getrawx ()) > 200) { +                     //swipe from right to left, slide to next page - Slidenext (); the                     return true; *                 } $                 return Super. Onfling (E1, E2, Velocityx, velocityy);Panax Notoginseng             } -         }); the     } +  A     //3. Using gesture recognizer the @Override +      Public Booleanontouchevent (Motionevent event) { - detector.ontouchevent (event); $         return Super. Ontouchevent (event); $     } -  -      Public voidNext (view view) { the Slidenext (); -     }Wuyi  the      Public voidPre (view view) { - Slidepre (); Wu     } -  About}
View Code

5. Let the class that needs to use the sliding switch interface inherit the newly created abstract class (slideactivity) and implement its two methods that are not implemented (slide up one step (Slidepre ()) and slide to Next (Slidenext ()). In both of these non-implemented methods, the interface sliding switch is realized by intent and animation effects. (Note that there is no previous step in the first step, so there is no need to write code after implementing the slide-up step (Slidepre ()) method.) )

Android instance-Mobile security Defender (23)-Custom abstract class and use

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.