通过拖放Android复制视图

时间:2013-07-23 14:33:46

标签: android

如何在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;
    }

1 个答案:

答案 0 :(得分:-2)

顺便说一下,你得到一个java.lang.IllegalStateException,声明"指定的孩子已经有了父母。"你必须做的是添加

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

当动作是DragEvent.ACTION_DROP。