kaddy
kaddy

Reputation: 119

onTouch() ACTION_MOVE case is not called while dragging

I have an ImageView which is listening to my touch . I am dragging the Image in ACTION_DOWN case. I am able to drag it but unable to detect the move event as it is never called .

card.setOnTouchListener(this); // inside onCreate()

public boolean onTouch(View view, MotionEvent motionEvent) {

        switch (motionEvent.getAction()) {
        case (MotionEvent.ACTION_DOWN):
            DragShadowBuilder shadowBuilder = new View.DragShadowBuilder(view);
            view.startDrag(null, shadowBuilder, view, 0);
            view.setVisibility(View.INVISIBLE);

            return true;

        case (MotionEvent.ACTION_MOVE):
            Log.v("pref", "sometimes");
            return true;
        }
        return true;
    }

Upvotes: 4

Views: 2516

Answers (1)

sled
sled

Reputation: 14625

Indeed, the startDrag(...) prevents your View from receiving further touch events. It seems like as soon as the drag and drop starts, an overlay is created, covering the whole screen and consuming all the touch events. Only the drag events will be sent to ALL views on your screen.

This can be found in startDrag's documentation :

Once the system has the drag shadow, it begins the drag and drop operation by sending drag events to all the View objects in your application that are currently visible. It does this either by calling the View object's drag listener (an implementation of onDrag() or by calling the View object's onDragEvent() method. Both are passed a DragEvent object that has a getAction() value of ACTION_DRAG_STARTED

How to handle the drag events can be found in this part of the documentation:

Drag and Drop - Handling events during the drag

Try to extend your existing code to catch the drag events:

// inside onCreate(), needs "implements View.OnDragListener"
card.setOnDragListener(this);

public boolean onDrag(View v, DragEvent event) {
    // Defines a variable to store the action type for the incoming event
    final int action = event.getAction();

    // Handles each of the expected events
    switch(action) {
        case DragEvent.ACTION_DRAG_LOCATION:
            final int x = event.getX();
            final int y = event.getY();
            Log.d("prefs", "X=" + String.valueOf(x) + " / Y=" + String.valueOf(y));
            break;
    }
    return true;
}

Upvotes: 2

Related Questions