Why android onLongPress always is fired after onDoubleTap?

后端 未结 2 889
南笙
南笙 2020-12-25 15:30

I have onLongPress and onDoubleTap actions placed on the button according to this code:

...
GestureDetector detector = new GestureDetector(this, new TapDetec         


        
相关标签:
2条回答
  • 2020-12-25 16:05

    Technically this shouldn't happen

    case MotionEvent.ACTION_DOWN:
            mLastMotionX = x;
            mLastMotionY = y;
            mCurrentDownEvent = MotionEvent.obtain(ev);
            mAlwaysInTapRegion = true;
            mInLongPress = false;
    
            if (mIsLongpressEnabled) {
                mHandler.removeMessages(LONG_PRESS);
                mHandler.sendEmptyMessageAtTime(LONG_PRESS, mCurrentDownEvent.getDownTime()
                        + tapTime + longpressTime);
            }
            mHandler.sendEmptyMessageAtTime(SHOW_PRESS, mCurrentDownEvent.getDownTime() + tapTime);
    

    because on the ACTION_DOWN or ACTION_UP event, all of the LONG_PRESS messages events are removed from the queue. So on the second tap the following code will remove the long press events.

     mHandler.removeMessages(LONG_PRESS);
    

    Ninja edit : hacky workaround for your problem

         @Override
         public void onLongPress(MotionEvent e) {
            if(MainActivity.this.hasWindowFocus())
            {
                Log.d("Touchy", "Long tap");    
            }
        }
    
    0 讨论(0)
  • 2020-12-25 16:07

    You see always the onLongPress fired because in your code you launch an intent before of consuming the onDoubleTap event.
    You can disable onLongPress by
    public void setIsLongpressEnabled (boolean isLongpressEnabled)
    and use the onDown method for performing your action.

    0 讨论(0)
提交回复
热议问题