Listening to Sliding Screen events with fingers on the left and right encapsulates a class

Source: Internet
Author: User

Sliding between the left and right is the most common action of a smartphone. It is encapsulated here and can be used directly in the future.


A few simple lines are required. The class below is encapsulated.

Package com. example. test; import android. OS. bundle; import android. app. activity; import android. content. context; import android. util. log; import android. widget. relativeLayout; public class MainActivity extends Activity {@ Overrideprotected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); // The xml here is a blank layout // viewRelativeLayout view that needs to listen to the left and right sliding events = (RelativeLayout) this. findViewById (R. id. layout); // setLongClickable is a required view. setLongClickable (true); view. setOnTouchListener (new MyGestureListener (this);}/*** inherits GestureListener and overwrites the left and right methods */private class MyGestureListener extends GestureListener {public MyGestureListener (Context context Context) {super (context) ;}@ Overridepublic boolean left () {Log. e ("test", "sliding left"); return super. left () ;}@ Overridepublic boolean right () {Log. e ("test", "slide right"); return super. right ();}}}

Package com. example. test; import android. content. context; import android. view. gestureDetector. simpleOnGestureListener; import android. view. gestureDetector; import android. view. view; import android. view. view. onTouchListener; import android. view. motionEvent;/*** monitors events that slide between the left and right, you can use * @ author LinZhiquan **/public class GestureListener extends SimpleOnGestureListener implements OnTouchListener {/** the shortest distance between the left and right slides */private int distance = 100; /** maximum Sliding Speed of left and right */private int velocity = 200; private GestureDetector gestureDetector; public GestureListener (Context context) {super (); gestureDetector = new GestureDetector (context, Context, this);}/*** indicates the method called when sliding to the left. The subclass should overwrite * @ return */public boolean left () {return false ;} /*** indicates the method called when sliding to the right. The subclass should override * @ return */public boolean right () {return false;} @ Overridepublic boolean onFling (MotionEvent e1, motionEvent e2, float velocityX, float velocityY) {// TODO Auto-generated method stub/e1: 1st ACTION_DOWN MotionEvent // e2: The last ACTION_MOVE MotionEvent // velocityX: moving speed on the X axis (pixel/second) // velocityY: moving speed on the Y axis (pixel/second) // sliding to the left if (e1.getX ()-e2.getX ()> distance & Math. abs (velocityX)> velocity) {left () ;}// slide right if (e2.getX ()-e1.getX ()> distance & Math. abs (velocityX)> velocity) {right ();} return false ;}@ Overridepublic boolean onTouch (View v, MotionEvent event) {// TODO Auto-generated method stubgestureDetector. onTouchEvent (event); return false;} public int getDistance () {return distance;} public void setDistance (int distance) {this. distance = distance;} public int getVelocity () {return velocity;} public void setVelocity (int velocity) {this. velocity = velocity;} public GestureDetector getGestureDetector () {return gestureDetector;} public void setGestureDetector (GestureDetector gestureDetector) {this. gestureDetector = gestureDetector ;}}


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.