Why android onLongPress always is fired after onDoubleTap?

后端 未结 2 901
南笙
南笙 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");    
            }
        }
    

提交回复
热议问题