Step-by-Step android development 72: slide right to close the Activity

Source: Internet
Author: User

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 ;}}

 

Related Article

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.