我有一个DragListener
我希望得到视图发布位置的坐标。但无论我放弃它,我都会得到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;
}
});
答案 0 :(得分:5)
我认为你的案子正在听错事件。 http://developer.android.com/reference/android/view/DragEvent.html#getX()
public float getX()
获取拖动点的X坐标。该值仅在事件操作为ACTION_DRAG_LOCATION或ACTION_DROP。
时有效
尝试将您的案例更改为DragEvent.ACTION_DROP
答案 1 :(得分:0)