0

As I know, if my onTouch method returns false, after the initial ACTION_DOWN case it does not start tracking the movement, and if I return true, it does. But I have some cases when I need to stop tracking the movement from some moment. How do I achieve this? I tried to returning false when my condition holds, but it continues to run into that case. How to cancel or stop the motion event?

Here is the code I have for my drag and drop application, and when the X is less than 100, I just want to stop dragging. How can I achieve this?

@Override
    public boolean onTouch(View v, MotionEvent event) {

        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                imageTouchX = (int) event.getRawX() - (int) img.getX();
                imageTouchY = (int) event.getRawY() - (int) img.getY();
                break;
            case MotionEvent.ACTION_MOVE:
                int x_cord = (int) event.getRawX();
                int y_cord = (int) event.getRawY();

                img.setX(x_cord - imageTouchX);
                img.setY(y_cord - imageTouchY);

                if(img.getX() < 100){
                    return false;
                }
                break;
            default:
                break;
        }
        return true;
    }

Thanks in advance.

vcmkrtchyan
  • 2,536
  • 5
  • 30
  • 59
  • A cleaner way would be to consume the event yet not make any changes i.e if (x_cord - imageTouchX<100){ img.setX(x_cord - imageTouchX); img.setY(y_cord - imageTouchY); } – humblerookie May 13 '15 at 03:10
  • yeah but i will still be able to continue dragging, when the condition does not hold...i guess the better way is to remove the ontouch listener from the object, as Ben Levi said, but that still brings some problems, because i want to stop dragging, but when another time i touch the image, i want for example some actions to be done via action_down... – vcmkrtchyan May 13 '15 at 11:16

1 Answers1

0

what if the img goes under the x location of 100 and then comes back ? one way to do it is to disable the touchevent all together, but then how you'll get the next touchevents ? unless it's going to be another view that will receive the touch event, anyhow, if you stop the touch events you won't receive any touch events again, and if this is what you wish to get, just disable the touchevent for this view, you can enable it again when you choose, good luck ! :)

Ben Levi
  • 177
  • 1
  • 5