我有一个 DragListener
,我想从中获取 View 释放位置的坐标。但无论我把它放在哪里,我都会得到 x = 0
和 y = 0
。显然我做错了什么。如何获取发布点的x
和y
?
this.setOnDragListener(new View.OnDragListener() {
CustomIcon temp;
CustomIcon dragging;
Boolean thisOne;
public boolean onDrag(View v, DragEvent event) {
for(int i= 0; i<mIcons.size();i++) {
temp = mIcons.get(i);
thisOne = temp.getDragging();
if(thisOne) {
dragging = temp;
}
}
final int action = event.getAction();
switch (action) {
case DragEvent.ACTION_DRAG_STARTED: {
Toast.makeText(getContext(), "Dragging: "+v+" Width:"+dragging.getWidth()+ " X:"+dragging.getX(),Toast.LENGTH_SHORT).show();
dragging.setVisibility(View.INVISIBLE);
} break;
case DragEvent.ACTION_DRAG_ENDED: {
dragging.setVisibility(View.VISIBLE);
dragging.stopDragging();
// WHERE MY PROBLEM IS:
Toast.makeText(getContext(), "Result: "+event.getResult()+" endX:"+(int) event.getX() + " endY:"+event.getY(),Toast.LENGTH_SHORT).show();
final boolean dropped = event.getResult();
} break;
}
return false;
}
});
最佳答案
我认为你的案例听错了事件。 http://developer.android.com/reference/android/view/DragEvent.html#getX ()
public float getX ()
Gets the X coordinate of the drag point. The value is only valid if the event action is ACTION_DRAG_LOCATION or ACTION_DROP.
尝试将您的案例更改为 DragEvent.ACTION_DROP
关于android - 如何获得拖动的释放 X 和 Y 位置?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9828187/