Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
552 views
in Technique[技术] by (71.8m points)

android - Stop OnLongClickListener from firing while dragging

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 such as reset position etc.

In the custom View I add my OnLongClickListener:

this.setOnLongClickListener(new View.OnLongClickListener() {
    @Override
    public boolean onLongClick(View v) {
        // show context menu..
        return true;
    }
});

And override onTouchEvent to look something like this:

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

The handleDrag function finds what object is been pressed, and handles updating it's position.

My problem is that when I start to drag an image the OnLongClickListener fires also. I'm not sure the best way around this.

I've tried adding a threshold to handleDrag to return false if user touches down but doesn't attempt to drag, but I'm finding it still difficult to get the correct handler fired.

Can anyone suggest a way to skip the OnLongClickListener while dragging?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...