Add left and right gesture recognition for activity

Source: Internet
Author: User

Added left and right gesture recognition for the activity in Android development, for example, sliding right to close the current page

/** For left and right gestures * 1. copy the following content to the target activity * 2. the oncreate () of the target activity calls initgesture () * 3. the target activity must be implements ontouchlistener, ongesturelistener */private listener; private int verticalmindistance = 180; private int minvelocity = 0; private void initgesture () {listener = new listener) this);} public Boolean onfling (motionevent E1, motionevent E2, float velocityx, float velocityy) {If (e1.getx ()-e2.getx ()> verticalmindistance & math. ABS (velocityx)> minvelocity) {// switch activity // intent = new intent (viewsnsactivity. this, updatestatusactivity. class); // startactivity (intent); // toast. maketext (this, "gesture to the left", toast. length_short ). show ();} else if (e2.getx ()-e1.getx ()> verticalmindistance & math. ABS (velocityx)> minvelocity) {// switch activity // intent = new intent (viewsnsactivity. this, updatestatusactivity. class); // startactivity (intent); // toast. maketext (this, "gesture to the right", toast. length_short ). show (); finish (); overridependingtransition (R. anim. push_right_in, R. anim. push_right_out);} return false;} @ override public void onlongpress (motionevent arg0) {// todo auto-generated method stub} @ override public Boolean onscroll (motionevent arg0, motionevent arg1, float arg2, float arg3) {// todo auto-generated method stub return false;} @ override public void onshowpress (motionevent arg0) {// todo auto-generated method stub} @ override public Boolean onsingletapup (motionevent arg0) {// todo auto-generated method stub return false ;} @ override public Boolean ontouch (view V, motionevent event) {// todo auto-generated method stub return mgesturedetector. ontouchevent (event) ;}@ override public Boolean ondown (motionevent arg0) {// todo auto-generated method stub return false;} @ override public Boolean dispatchtouchevent (motionevent eV) {mgesturedetector. ontouchevent (EV); return Super. dispatchtouchevent (EV );}


Add left and right gesture recognition for activity

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.