Step-by-Step android development 72: slide right to close the Activity
You can use the right-sliding gesture to close the current Activity. This is a common requirement. I have referenced several articles on the Internet and found that the implementation is complicated. Most of them write a Layout by themselves, then this Layout is used as the page Layout. After testing, there is actually a simpler method to write a BaseActivity, and other activities can be inherited. The code is provided here for testing. Please give back if you have any questions. Thank you!
Package com. figo. study. activity; import android. app. activity; import android. OS. bundle; import android. view. gestureDetector; import android. view. motionEvent; import android. view. view; public class BaseActivity extends Activity implements View. onTouchListener, GestureDetector. onGestureListener {GestureDetector mGestureDetector; @ Override public boolean onTouch (View v, MotionEvent event) {return mGestureDetector. onTouchEvent (event) ;}@ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); mGestureDetector = new GestureDetector (GestureDetector. onGestureListener) this);} @ Override public boolean dispatchTouchEvent (MotionEvent ev) {mGestureDetector. onTouchEvent (ev); return super. dispatchTouchEvent (ev) ;}@ Override public boolean onDown (MotionEvent e) {return false ;}@ Override public void onShowPress (MotionEvent e) {}@ Override public boolean onSingleTapUp (MotionEvent e) {return false ;}@ Override public boolean onScroll (MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {return false ;}@ Override public void onLongPress (MotionEvent e) {} private int verticalMinDistance = 20; private int minVelocity = 0; @ Override public boolean onFling (MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {if (Math. abs (e2.getY ()-e1.getY ()> 200) {// This is to prevent the return false function from being disabled on a page with scroolview;} if (e1.getX ()-e2.getX ()> verticalMinDistance & Math. abs (velocityX)> minVelocity) {// left slide operation} else if (e2.getX ()-e1.getX ()> verticalMinDistance & Math. abs (velocityX)> minVelocity) {// right-Sliding operation this. finish () ;}return false ;}}