我在屏幕上使用Android Drag and Drop
来drag
我的图像。当我drop
时,图像会返回其原始位置。但是我希望图像留在它被放入的位置。
我的尝试:我在ACTION_DROP
保存了图像的X和Y位置,然后尝试使用相同的X和Y坐标设置新的view
位置。
有人可以帮忙吗?
如果我需要提供更多代码,请告诉我。
代码:
class MyDragListener implements View.OnDragListener {
@Override
public boolean onDrag(View v, DragEvent event) {
int action = event.getAction();
switch (event.getAction()) {
case DragEvent.ACTION_DRAG_STARTED:
break;
case DragEvent.ACTION_DRAG_ENTERED:
if(v==findViewById(R.id.linear_layout_button))
findViewById(R.id.removeText).setVisibility(View.VISIBLE);
break;
case DragEvent.ACTION_DRAG_EXITED:
if(v==findViewById(R.id.linear_layout_button))
findViewById(R.id.removeText).setVisibility(View.GONE);
break;
case DragEvent.ACTION_DROP:
if(v==findViewById(R.id.linear_layout_image)) {
View view = (View) event.getLocalState();
float posx = view.getX();
float posy = view.getY();
ViewGroup owner = (ViewGroup) view.getParent();
owner.removeView(view);
LinearLayout container = (LinearLayout) v;
container.addView(view);
view.setX(posx);
view.setY(posy);
view.setVisibility(View.VISIBLE);
}
break;
case DragEvent.ACTION_DRAG_ENDED:
default:
break;
}
return true;
}
}
答案 0 :(得分:0)
这种情况正在发生,因为您再次使用x y位置。试着评论这两行
// view.setX(posx);
// view.setY(posy);