Double-click event listening for Activity (solving event dispatching issues)

Source: Internet
Author: User
Tags xdiff

The GestureDetector of the gesture recognition class can be used to implement double-click listening using the standard SDK class, but there is a limitation: Because GD intercepts events in the OnTouchEvent method for processing, and Android event dispatching process, in this way, if the View inside the Activity consumes click events, GD will not be able to accept click events. To this end, I have implemented a tool class that can perfectly listen to Activity events: DoubleClick. All source code of the class: [java] public class DoubleClick {private int clickCount; private long firstClickTime; private int CLICK_DELAY = 300; private final static int MOVE_OFFSET = 20; private float mLastMotionY; private float mLastMotionX; private Timer cleanClickTimer = new Timer (); private OnDoubleClickListener listener; public DoubleClick (OnDoubleClickListener l) {listener = l;} public interface OnDoubleClick Listener {void onDoubleClick ();} public void dispatchTouchEvent (MotionEvent event) {final float y = event. getY (); final float x = event. getX (); if (event. getAction () = MotionEvent. ACTION_DOWN) {mLastMotionY = y; mLastMotionX = x; clickCount ++; if (clickCount = 1) {firstClickTime = System. currentTimeMillis (); // The number of clicks that have not been clicked again after the listening time is 50 ms. The click event is cleared. CleanClickTimer. schedule (new TimerTask () {@ Override public void run () {clickCount = 0; firstClickTime = 0 ;}}, CLICK_DELAY + 50);} else if (clickCount = 2) {long secondClickTime = System. currentTimeMillis (); if (secondClickTime-firstClickTime <= CLICK_DELAY) {listener. onDoubleClick () ;}clickcount = 0; firstClickTime = 0 ;}} if (event. getAction () = MotionEvent. ACTION_MOVE) {final int yDiff = (Int) Math. abs (y-mLastMotionY); final int xDiff = (int) Math. abs (x-mLastMotionX); boolean yMoved = yDiff> MOVE_OFFSET; boolean xMoved = xDiff> MOVE_OFFSET; // judge whether the value is moving if (yMoved | xMoved) {clickCount = 0; firstClickTime = 0 ;}}} use an instance: [java] private DoubleClick doubleClick; // call this method when you need to use double-click event listening. Protected void enableDClickReturn () {doubleClick = new DoubleClick (new DoubleClick. onDoubleClickListener () {@ Override public void onDoubleClick () {// here, the function required by my project is to double-click to return the previous level of Activity finish ();}});} @ Override public boolean dispatchTouchEvent (MotionEvent event) {if (doubleClick = null) {return super. dispatchTouchEvent (event);} else {doubleClick. dispatchTouchEvent (event);} return super. dispatchTou ChEvent (event);} for more Android project tool classes, follow your GitHub homepage. Except for the company project source code can not be open, all my source code is open source to Github: http://github.com/chenyoca

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.