You can slide to the right to return the Activity in a few simple steps.
Sliding to the right is a good user experience for mobile phones with too many screens, you can click the return button in the upper-left corner of the screen or click the return button in the lower-right corner of the phone to return to the previous page, most apps support this function now. Does your APP support it?
On the Internet, Baidu has some sliding return methods, some of which are third-party controls such as swipebackLayout, but the disadvantages are too large, such as conflicts with custom controls, some of them are through gesture monitoring, but the steps are quite complicated. In short, they are not satisfactory. The implementation method described in this article is actually implemented by listening for event distribution, but the steps are very simple, the results have been continuously tested by myself. Next I will explain the implementation process:
To implement this function, you must satisfy several conditions at the same time and consider the user's operation intention. You must ensure sufficient sensitivity and avoid sliding to the right for many times without returning the previous page, if you want to slide up and down (slide up and down) rather than slide to the right, you are also judged to have ended the current interface by swiping to the right, so we need to meet the following requirements:
1. You need to slide a distance to the right, and the X axis distance> a specified value;
2. the offset to the Y axis cannot exceed a specified value because it cannot be scaled in the horizontal direction strictly without the Y axis, otherwise, the user's intention is not to slide back but to slide up and down;
3. during the test, if the user intends to slide up or down, the speed of the finger moving on the Y axis (we can calculate the value of the pixel moving per second through the VelocityTracker class) is very large, it usually ranges from several thousand to tens of thousands, while in normal horizontal sliding, the moving speed of the Y axis is usually only about 100. Therefore, what we need to judge is, if the Sliding Speed of the finger on the Y axis exceeds a certain value (I set this value to 1000), the user intends to slide up and down rather than return to the right;
Now, we can customize a SlideBackActivity to inherit the Activity, and re-write the dispatchTouchEvent distribution in the SlideBackActivity, and record the finger press, moving distance, and sliding speed, to determine the user's intention, complete code:
/* ** Supports sliding return * inherits the Activity and supports sliding return * @ author */public class SlideBackActivity extends Activity {// The minimum speed of private static final when the finger slides up or down. int YSPEED_MIN = 1000; // The minimum private static final int XDISTANCE_MIN = 50 when the finger slides to the right; // The minimum private static final int YDISTANCE_MIN = 100 when the finger slides up or down; // record the abscissa when the finger is pressed. Private float xDown; // record the ordinate when the finger is pressed. Private float yDown; // records the abscissa when the finger moves. Private float xMove; // record the ordinate value when the finger moves. Private float yMove; // used to calculate the speed at which the finger slides. Private VelocityTracker mVelocityTracker; @ Override public boolean dispatchTouchEvent (MotionEvent event) {createVelocityTracker (event); switch (event. getAction () {case MotionEvent. ACTION_DOWN: xDown = event. getRawX (); yDown = event. getRawY (); break; case MotionEvent. ACTION_MOVE: xMove = event. getRawX (); yMove = event. getRawY (); // The sliding distance from int distanceX = (int) (xMove-xDown); int distanceY = (int) (YMove-yDown); // get the speed int ySpeed = getScrollVelocity (); // The following conditions must be met to disable the Activity: // 1. X axis Sliding distance> XDISTANCE_MIN // 2. the Y-axis Sliding distance is within the YDISTANCE_MIN range // 3. on the Y axis (that is, the sliding speed) <XSPEED_MIN. If it is greater, the user intends to slide up and down rather than Slide left to end Activity if (distanceX> XDISTANCE_MIN & (distanceY <YDISTANCE_MIN & distanceY>-YDISTANCE_MIN) & ySpeed <YSPEED_MIN) {finish ();} break; case MotionEvent. ACTION_UP: recycleVelocityTracker (); break; default: break ;} Return super. dispatchTouchEvent (event);}/*** creates a VelocityTracker object and adds the sliding event of the touch interface to the VelocityTracker. ** @ Param event **/private void createVelocityTracker (MotionEvent event) {if (mVelocityTracker = null) {mVelocityTracker = VelocityTracker. obtain ();} mVelocityTracker. addMovement (event);}/*** recycles the VelocityTracker object. */Private void recycleVelocityTracker () {mVelocityTracker. recycle (); mVelocityTracker = null;}/*** @ return sliding speed, measured in the number of pixels that are moved per second. */Private int getScrollVelocity () {mVelocityTracker. computeCurrentVelocity (1000); int velocity = (int) mVelocityTracker. getYVelocity (); return Math. abs (velocity );}}