Requirement: When viewpager is used to display an image, the image needs to be scaled when you double-click it. popwindow is displayed at the bottom of the screen when you click it. Because android double-click is essentially a two-click operation, however, I don't want to trigger the clicking action when I double-click it. So I searched for solutions on the Internet. However, after painstaking consideration, I found the following methods:
1. Override the dispatchTouchEvent method of the activity.
1 @Override 2 public boolean dispatchTouchEvent(MotionEvent event) { 3 4 if (event.getAction() == MotionEvent.ACTION_DOWN) { 5 mDownX = (int) event.getX(); 6 if (mFirstTouchEventTime == 0) { 7 mFirstTouchEventTime = event.getDownTime(); 8 } else { 9 mLastTouchEventTime = event.getDownTime();10 }11 } else if (event.getAction() == MotionEvent.ACTION_UP) {12 mUpX = (int) event.getX();13 if (Math.abs(mUpX - mDownX) < 30) {14 Log.d(TAG, "--->>" + Math.abs(mFirstTouchEventTime - mLastTouchEventTime));15 if (Math.abs(mFirstTouchEventTime - mLastTouchEventTime) < Constants.TIME_MENU_DISPLAY) {16 mFirstTouchEventTime = 0L;17 mLastTouchEventTime = 0L;18 mScreenObsever.removeMessages(Constants.MSG_MENU_DISPLAY);19 } else {20 mScreenObsever.sendEmptyMessageDelayed(Constants.MSG_MENU_DISPLAY, Constants.TIME_MENU_DISPLAY);21 }22 } else {23 mFirstTouchEventTime = 0L;24 mLastTouchEventTime = 0L;25 }26 }27 28 return super.dispatchTouchEvent(event);29 }
MFirstTouchEventTime and mLastTouchEventTime are two long variables used to record the time when two ACTION_DOWN actions occur. Constants. TIME_MENU_DISPLAY is a custom msg what. MScreenObserver is a custom handler used to process click events.
2. handler class implementation of mScreenObserver
private class ObseverOperateHandler extends Handler {@Overridepublic 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 part in if is to be processed in the click.
3. As for the double-click Task, You can implement GestureDetector. OnDoubleTapListener normally. You can find out how to get it online.
In addition, the Constants. TIME_MENU_DISPLAY Time Value in the Code is verified as MS in various ways, that is, the two-click interval MS is considered as a double-click.