Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Drag & Drop without removal from owner

I am using on a LinearLayout with three TextViews Drag&Drop to move it to another container. My code:

OnDragListener dragi = new OnDragListener() {

            @Override
            public boolean onDrag(View arg0, DragEvent arg1) {
                // TODO Auto-generated method stub
                int action = arg1.getAction();
                switch (action) {
                case DragEvent.ACTION_DRAG_STARTED:

                break;
                case DragEvent.ACTION_DRAG_ENTERED:

                break;
                case DragEvent.ACTION_DRAG_EXITED:

                break;
                case DragEvent.ACTION_DROP:

                View view = (View) arg1.getLocalState();
                  ViewGroup owner = (ViewGroup) view.getParent();
                  owner.removeView(view);
                  //
                  LinearLayout container = (LinearLayout) arg0;
                  container.addView(view);
                  view.setVisibility(View.VISIBLE);

                  }
                  break;
                case DragEvent.ACTION_DRAG_ENDED:

                  break;
                default:
                  break;
                }
                return true;
            }
        };

My Problem is that i dont want to remove the view from the base container, it should stay there and just add a copy to the second container.

Thx

like image 831
user547995 Avatar asked Feb 17 '14 12:02

user547995


1 Answers

A view can only have one parent at any given moment, what you can do is on your drag, instead of dropping the view into the new container create a clone of the view and add that on to the new container.

To clone a view: if your view => Button called myButton, then you can do this:

Button myButtonClone = new Button(context);
myButtonClone.setText(myButton.getText());

You will have a clone of the original button. This applies to every other property, if you want to make a clone of an ImageView simply create a new one and set its drawable to be the same one as the first ImageView..

like image 60
nunoh123 Avatar answered Sep 20 '22 21:09

nunoh123