Two steps to the right of activity to swipe back to the function

Source: Internet
Author: User

Swipe right to return, for the screen is too large for the phone, in one-handed operation, is a good user experience, the user does not have to work hard or with another hand to tap the back button in the upper left corner of the screen or, the phone lower right corner of the back button, gently swipe to the right of the screen to return to the previous page, This feature is now supported by most apps, does your app support it?

Baidu on the Internet some of the methods of sliding back, there are some third-party controls such as Swipebacklayout but the disadvantages too large as with their own custom some of the control conflicts, some through the judgment gesture monitoring but the steps are quite cumbersome, in short, not satisfactory, The implementation method described in this article is actually through the monitoring of event distribution to achieve, but the steps are very simple, and the effect of my constant testing is quite good, next I explain the implementation process:

The implementation of the function needs to meet several conditions, and to consider the user's operating intentions, both to ensure sufficient sensitivity, do not appear to the right to slide several times not return to the previous page of the situation, do not want to go up and down (oblique up and down) instead of sliding to the right to return, is also judged to the right to return to the end Then we need to meet:

1. The user should swipe to the right for a distance, and the x-axis distance > a set value;

2. Because the right to slide, it is not possible to strictly horizontal direction without offset to the y-axis, so the offset to the y-axis can not exceed a set value, otherwise it is considered that the user intent is not to slide back, but sliding up and down;

3. During testing, if the user intends to swipe up and down, then the finger moves at the y-axis (we can calculate by the Velocitytracker class by the number of pixels moving per second), usually in the thousands of to the million, and at normal horizontal sliding, the y-axis movement speed is usually only about 100, So what we need to determine is that if the finger slips faster than a certain value on the y-axis (I set the value to 1000), the user's intention is to swipe up and down rather than to the right to return;

Well, next we can customize a slidebackactivity to inherit activity, and rewrite the event distribution dispatchtouchevent in Slidebackactivity, and record the finger press, the distance to move and the speed of the finger sliding, Thus judging the user's intentions, the complete code:

/*** Support Sliding back * Inherit the activity then support sliding back*/ Public classSlidebackactivityextendsactivity{//the minimum speed when the finger slides up and down     Private Static Final intYspeed_min = 1000; //the minimum distance when the finger is sliding to the right     Private Static Final intXdistance_min = 50; //the minimum distance when the finger slides upward or falls     Private Static Final intYdistance_min = 100; //record the horizontal axis of the finger when it is pressed.      Private floatXdown; //records the ordinate when the finger is pressed.      Private floatYdown; //record the horizontal axis of the finger when moving.      Private floatXmove; //records the ordinate when the finger moves.      Private floatYmove; //used to calculate the speed of finger slippage.      PrivateVelocitytracker Mvelocitytracker; @Override Public Booleandispatchtouchevent (Motionevent event) {Createvelocitytracker (event); Switch(Event.getaction ()) { CaseMotionEvent.ACTION_DOWN:xDown=event.getrawx (); Ydown=Event.getrawy ();  Break;  CaseMotionEvent.ACTION_MOVE:xMove=event.getrawx (); Ymove=Event.getrawy (); //Distance to Slide                intDistancex = (int) (Xmove-Xdown); intDistancey= (int) (Ymove-Ydown); //Get time Speed                intYspeed =getscrollvelocity (); //The following conditions must be met to close activity://distance >xdistance_min of 1.x axis sliding//2.y axis sliding distance within the Ydistance_min range//on the 3.Y axis (that is, the speed up and down) <xspeed_min, if it is greater than the user intent is to swipe up and down rather than left to end activity                if(Distancex > Xdistance_min && (distancey<ydistance_min&&distancey>-ydistance_min) & & Yspeed <yspeed_min)                {Finish (); }                 Break;  CaseMotionEvent.ACTION_UP:recycleVelocityTracker ();  Break; default:                 Break; }          return Super. Dispatchtouchevent (event); }     /*** Create a Velocitytracker object and add the swipe events of the touch interface to the Velocitytracker. *      * @paramEvent **/    Private voidCreatevelocitytracker (Motionevent event) {if(Mvelocitytracker = =NULL) {Mvelocitytracker=Velocitytracker.obtain ();    } mvelocitytracker.addmovement (event); }     /*** Reclaim Velocitytracker objects. */    Private voidRecyclevelocitytracker () {mvelocitytracker.recycle (); Mvelocitytracker=NULL; }     /**      *      * @returnslide speed, in units of how many pixel values are moved per second. */    Private intgetscrollvelocity () {mvelocitytracker.computecurrentvelocity (1000); intVelocity = (int) mvelocitytracker.getyvelocity (); returnmath.abs (velocity); } }

The next two steps make it easy to add this feature to your app:

The first step: Create a class named Slidebackactivity, and copy and paste the above code.

Part II: The page where your app needs to swipe to the right returns the activity inheritance to slidebackactivity, which can be implemented.

Reproduced in: Android Development Chinese station»activity simple steps support right swipe back

Two steps to get the activity to the right swipe back to the function

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.