1

如何在 Android 上将视图(例如图像)从一个面板复制到另一个面板?我试过:

case DragEvent.ACTION_DROP:

                    View view = (View) event.getLocalState();
                    LinearLayout container = (LinearLayout) v;
                    container.addView(view);
                    break;

但是,它不起作用。
这是 onDrag() 函数,我的应用程序退出Exception

    public boolean onDrag(View v, DragEvent event) {
        int action = event.getAction();
        switch (event.getAction()) {
            case DragEvent.ACTION_DRAG_STARTED:
                // Do nothing
                break;
            case DragEvent.ACTION_DRAG_ENTERED:
                v.setBackgroundDrawable(enterShape);
                break;
            case DragEvent.ACTION_DRAG_EXITED:
                v.setBackgroundDrawable(normalShape);
                break;
            case DragEvent.ACTION_DROP:
                // add view 
                View view = (View) event.getLocalState();
                LinearLayout container = (LinearLayout) v;
                container.addView(view);                    
                break;
            case DragEvent.ACTION_DRAG_ENDED:
                v.setBackgroundDrawable(normalShape);
            default:
                break;
        }
        return true;
    }
4

1 回答 1

-2

您可能会收到一个 java.lang.IllegalStateException,说明“指定的孩子已经有一个父母”。你必须做的是添加

View view = (View) event.getLocalState();
ViewGroup owner = (ViewGroup) view.getParent();
owner.removeView(view);

当动作是 DragEvent.ACTION_DROP 时。

于 2013-08-01T09:41:21.463 回答