Reputation: 37
I'm struggling at combining DragListener with SingeTap action in an imageView. They work fine separately but how this two should be combined?
imageView.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
switch(event.getAction()) {
case MotionEvent.ACTION_UP:
Log.e("SINGLE TAP","?");
break;
case MotionEvent.ACTION_MOVE:
ClipData data = ClipData.newPlainText("TRYING", "");
DragShadowBuilder shadowBuilder = new View.DragShadowBuilder(v);
v.startDrag(data, shadowBuilder, v, 0);
break;
default: break;
}
return false;
}
});
Still doesn't seem to work. How this can be achieved?
Upvotes: 0
Views: 303
Reputation: 4471
ACTION_UP
will be called after a drag event finishes, as well as after a tap event finishes. Therefore you should have a flag that detects if a drag event occurred or not. Here is an example
imageView.setOnTouchListener(new OnTouchListener() {
private boolean isDrag = false;
@Override
public boolean onTouch(View v, MotionEvent event) {
switch(event.getAction()) {
case MotionEvent.ACTION_UP:
if (!isDrag) {
// action was a single tap
}
isDrag = false; // reset the flag
break;
case MotionEvent.ACTION_MOVE:
isDrag = true; // set the flag
ClipData data = ClipData.newPlainText("TRYING", "");
DragShadowBuilder shadowBuilder = new View.DragShadowBuilder(v);
v.startDrag(data, shadowBuilder, v, 0);
break;
default: break;
}
return false;
}
});
Upvotes: 3