[英]How do I get the release X and Y position of a Drag?
I have a DragListener
of which I would like to get the coordinates of where the view is released. 我有一个DragListener
,我想获取释放视图的位置的坐标。 But no matter where I drop it I get x = 0
and y = 0
. 但是无论我把它放在哪里,我都得到x = 0
和y = 0
。 Obviously I am doing something wrong. 显然我做错了。 How do I get the x
and y
of the release point? 如何获得释放点的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;
}
});
I think your case is listening to the wrong event. 我认为您的案件正在听错事件。 http://developer.android.com/reference/android/view/DragEvent.html#getX () http://developer.android.com/reference/android/view/DragEvent.html#getX ()
public float getX () 公共浮动getX()
Gets the X coordinate of the drag point. 获取拖动点的X坐标。 The value is only valid if the event action is ACTION_DRAG_LOCATION or ACTION_DROP. 该值仅在事件操作为ACTION_DRAG_LOCATION或ACTION_DROP时有效。
Try changing your case to DragEvent.ACTION_DROP 尝试将大小写更改为DragEvent.ACTION_DROP
i had a problem like this. 我有这样的问题。 there is 3 important thing 1- set drag listener on parent view(for ex: linear layout or relative or .. ) 2- use ACTION_DROP for taking cooridinate x,y and not ACTION_DRAG_ENDED 3- set startDrag() on view that you want to drag it over parent. 有3件重要的事情1-在父视图上设置拖动侦听器(例如:线性布局或相对或..)2-使用ACTION_DROP来获取x,y的坐标,而不是ACTION_DRAG_ENDED 3-在您想要的视图上设置startDrag()将其拖到父对象上。 for example you can use setonlongclick() and set starDrag() inside longclick event 例如,您可以使用setonlongclick()并在longclick事件中设置starDrag()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.