Stop OnLongClickListener from firing while dragging

后端 未结 3 809
忘了有多久
忘了有多久 2020-12-17 20:46

I have a custom View with bitmaps on it that the user can drag about.

I want to make it so when they long click one of them I can pop up a context menu with options

3条回答
  •  半阙折子戏
    2020-12-17 21:21

    I think I have this solved through tweaking my threshold approach.

    First, I changed my onTouchEvent to look like this:

     public boolean onTouchEvent(MotionEvent event) {
         mMultiTouchController.handleDrag(event);
         return super.onTouchEvent(event);
     }
    

    They now both fire, so I then changed my OnLongClickListener to the following:

     this.setOnLongClickListener(new View.OnLongClickListener() {
         @Override
         public boolean onLongClick(View v) {
             if (!mMultiTouchController.has_moved) {
                 // Pop menu and done...
                 return false;
             }
             return true;
         }
     });
    

    (mMultiTouchController is the class containing all my gesture detection code). The key here is within this class, I added the bool 'has_moved'. When I go to start a drag I then compute the delta:

     float diffX = Math.abs(mCurrPtX - mPrevPt.getX());
     float diffY = Math.abs(mCurrPtY - mPrevPt.getY());
     if (diffX < threshold && diffY < threshold) {
         has_moved = false;
         return;
     }
    

    Now when the onLongClick fires I know whether to take action or not.

    The final piece was to set:

    setHapticFeedbackEnabled(false);
    

    in my View so that the user doesn't get a vibrate every time the longClick fires but no action is taken. I plan to do the vibration manually as a next step.

    This seems to be ok so far, hope that helps anyone who has come across a similar situation as this one.

提交回复
热议问题