Android completely distinguishes between Double-tap and Singal-tap methods

Source: Internet
Author: User

Requirements: Viewpager display pictures, you need to double-click on the picture to zoom, when clicked on the bottom of the screen pop Popwindow, because the Android double-click Essence is two clicks, but do not want to trigger the click when the action, so on the internet a variety of search solutions, no results, After a while, found the following methods

1. Dispatchtouchevent method for overriding activity

1 @Override2      Public Booleandispatchtouchevent (Motionevent event) {3 4         if(event.getaction () = =Motionevent.action_down) {5Mdownx = (int) Event.getx ();6             if(Mfirsttoucheventtime = = 0) {7Mfirsttoucheventtime =event.getdowntime ();8}Else {9Mlasttoucheventtime =event.getdowntime ();Ten             } One}Else if(event.getaction () = =motionevent.action_up) { AMUPX = (int) Event.getx (); -             if(Math.Abs (MUPX-MDOWNX) < 30) { -LOG.D (TAG, "--->>" + math.abs (Mfirsttoucheventtime-mlasttoucheventtime)); the                 if(Math.Abs (Mfirsttoucheventtime-mlasttoucheventtime) <Constants.time_menu_display) { -Mfirsttoucheventtime = 0L; -Mlasttoucheventtime = 0L; - mscreenobsever.removemessages (constants.msg_menu_display); +}Else { - mscreenobsever.sendemptymessagedelayed (Constants.msg_menu_display, constants.time_menu_display); 
     +                 } A}Else { atMfirsttoucheventtime = 0L; -Mlasttoucheventtime = 0L; -             } -         } -  -         return Super. Dispatchtouchevent (event); in}
Among them, Mfirsttoucheventtime and Mlasttoucheventtime are two long class variables, which are used to record the time of the occurrence of two action_down actions,constants.time_menu_display for a custom msg what. Mscreenobserver is a custom handler that is used to handle click events.
Handler class implementation of 2.mScreenObserver
?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 private class ObseverOperateHandler extends Handler {         @Override         public void handleMessage(Message msg) {                if (msg.what == Constants.MSG_MENU_DISPLAY) {                 mFirstTouchEventTime = 0L;                 mLastTouchEventTime = 0L;                 if (mOperateMenuPopupWindow.isShowing()) {                     mNotePopWindow.startDownAnimation();                     mOperateMenuPopupWindow.dismiss();                 } else {                     mNotePopWindow.startUpAnimation();                     mOperateMenuPopupWindow.showAtLocation(findViewById(R.id.photoLayout), Gravity.BOTTOM, 0, 0);                 }             }             super.handleMessage(msg);         }     }

The if part is going to be in a single-hit deal.

3. As for the double-click to do things, you can normally implement Gesturedetector.ondoubletaplistener to deal with. You can find out how to do it online.

In addition the code of Constants.time_menu_display this time value, by various means to verify that the 350ms is more appropriate, that is, two-click Interval 350ms will be considered a double-click.

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.