拖放 - 图像返回原始位置

时间:2015-02-16 16:43:12

标签: android button drag-and-drop android-linearlayout

我在屏幕上使用Android Drag and Dropdrag我的图像。当我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;
    }
}

1 个答案:

答案 0 :(得分:0)

这种情况正在发生,因为您再次使用x y位置。试着评论这两行

   // view.setX(posx);
// view.setY(posy);