如何使onDragListener和onTouchListener一起工作? [英] How to make onDragListener and onTouchListener work together?
问题描述
我使用onDragListener在屏幕上拖动了一个对象,这部分效果很好.但是我还需要在拖动时检查屏幕的x,y坐标.我重写了onTouch方法,它也很好用.但是一旦我拖动对象,ontouch侦听器将无法工作.我不能让两个听众一起工作.我不明白为什么它不能一起工作.
I used onDragListener to drag an object on the screen, and this part works well. But i need also to check the x,y coordinates of the screen while dragging. I override onTouch method and it works well also. but once i dragged the object the ontouch listener doesn't work. I can't make the two listener work together. I don't why it doesn't work together.
以下用于拖动的代码
ima.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
ClipData.Item item = new ClipData.Item((CharSequence)v.getTag());
String[] mimeTypes = {ClipDescription.MIMETYPE_TEXT_PLAIN};
ClipData dragData = new ClipData(v.getTag().toString(),
mimeTypes, item);
// Instantiates the drag shadow builder.
View.DragShadowBuilder myShadow = new DragShadowBuilder(ima);
// Starts the drag
v.startDrag(dragData, // the data to be dragged
myShadow, // the drag shadow builder
null, // no need to use local data
0 // flags (not currently used, set to 0)
);
return true;
}
});
// Create and set the drag event listener for the View
ima.setOnDragListener( new OnDragListener(){
@Override
public boolean onDrag(View v, DragEvent event){
switch(event.getAction())
{
case DragEvent.ACTION_DRAG_STARTED:
System.out.println("Action is DragEvent.ACTION_DRAG_STARTED");
break;
case DragEvent.ACTION_DRAG_ENTERED:
Log.d(msg, "Action is DragEvent.ACTION_DRAG_ENTERED");
break;
case DragEvent.ACTION_DRAG_EXITED :
Log.d(msg, "Action is DragEvent.ACTION_DRAG_EXITED");
break;
case DragEvent.ACTION_DRAG_LOCATION :
Log.d(msg, "Action is DragEvent.ACTION_DRAG_LOCATION");
right=ima.getRight();
left=ima.getLeft();
top=ima.getTop();
bottom=ima.getBottom();
System.out.println("Start Touch "+right+" "+top+" "+left+" "+bottom);
/* if(x_cord>left&&y_cord>top&&x_cord<right&&y_cord<bottom){
System.out.println("GONE");
ima.setVisibility(View.GONE);
}*/
break;
case DragEvent.ACTION_DRAG_ENDED :
System.out.println( "ACTION_DRAG_ENDED event");
break;
case DragEvent.ACTION_DROP:
Log.d(msg, "ACTION_DROP event");
break;
default: break;
}
return true;
}
});
触摸侦听器的以下代码
@Override
public boolean onTouchEvent(MotionEvent event) {
final int action = event.getAction();
switch (action & MotionEvent.ACTION_MASK) {
case MotionEvent.ACTION_DOWN: {
x_cord = (int) event.getX();
y_cord = (int) event.getY();
String text = "You click at x = " + event.getX() + " and y = " + event.getY();
//Toast.makeText(this, text, Toast.LENGTH_LONG).show();
System.out.println(text);
break;
}
case MotionEvent.ACTION_MOVE:{
x_cord = (int) event.getX();
y_cord = (int) event.getY();
String text = "You click at x = " + event.getX() + " and y = " + event.getY();
// Toast.makeText(this, text, Toast.LENGTH_LONG).show();
System.out.println(text);
break;
}
}
return true;
}
推荐答案
将(相同的)拖动侦听器也放置在填充整个屏幕的视图中,这不是到处都调用ACTION_DRAG_LOCATION吗? (在这种情况下,您将不需要触摸监听器)
Putting the (same) draglistener also on a view that fills your whole screen, doesn't that call ACTION_DRAG_LOCATION everywhere? (and you won't need the touch listener in that case)
这篇关于如何使onDragListener和onTouchListener一起工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!