[英]How do I get the release X and Y position of a Drag?
我有一個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;
}
});
我認為您的案件正在聽錯事件。 http://developer.android.com/reference/android/view/DragEvent.html#getX ()
公共浮動getX()
獲取拖動點的X坐標。 該值僅在事件操作為ACTION_DRAG_LOCATION或ACTION_DROP時有效。
嘗試將大小寫更改為DragEvent.ACTION_DROP
我有這樣的問題。 有3件重要的事情1-在父視圖上設置拖動偵聽器(例如:線性布局或相對或..)2-使用ACTION_DROP來獲取x,y的坐標,而不是ACTION_DRAG_ENDED 3-在您想要的視圖上設置startDrag()將其拖到父對象上。 例如,您可以使用setonlongclick()並在longclick事件中設置starDrag()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.