Drag n Drop - Android

Asked

Viewed 53 times

0

I made a Drag n Drop system, but when I insert the item, it seems that gravity is like top, when inserting new ones, they stand on top of each other, like drop where I drop the finger?

The items are in a Listview, I get them like this:

public void criarListagem(List<croquiA> cro) {
        CroquiAdapter adapter = new CroquiAdapter(cro, this);
        listView.setAdapter(adapter);
        listView.setDivider(null);

        listView.setOnItemLongClickListener((parent, view, position, id) -> {
            arrayClass.p = arrayClass.pistasIds[position];
            ClipData data = ClipData.newPlainText("simple_text", "text");
            View.DragShadowBuilder shadowBuilder = new View.DragShadowBuilder(view);
            view.startDrag(data, shadowBuilder, view, 0);
            view.setVisibility(View.VISIBLE);

            newClick(arrayClass.p, l);

            if(l != 0){
                findViewById(R.id.container).setOnDragListener(new MyOnDragListener());
            }

            return false;
        });

Myondraglistener:

class MyOnDragListener implements View.OnDragListener{

        public MyOnDragListener(){
            super();
        }

        @Override
        public boolean onDrag(View v, DragEvent event) {
            int action = event.getAction();

            switch (action){
                case DragEvent.ACTION_DRAG_STARTED:
                   if(event.getClipDescription().hasMimeType(ClipDescription.MIMETYPE_TEXT_PLAIN)){
                       return true;
                   }else{
                       return false;
                   }

                case DragEvent.ACTION_DRAG_ENTERED:

                    break;

                case DragEvent.ACTION_DRAG_LOCATION:
                    break;

                case DragEvent.ACTION_DRAG_EXITED:
                    break;

                case DragEvent.ACTION_DROP:

                    View view = (View) event.getLocalState();
                    RelativeLayout container = (RelativeLayout) v;

                    // Aqui eu copio a view e adiciono um ImageView
                    // em vez de remover, porque como é um adaptador, então é
                    // não é possível removê-lo

                    ImageView oldView = (ImageView) view;
                    ImageView newView = new ImageView(getApplicationContext());

                    newView.setImageBitmap(((BitmapDrawable) oldView.getDrawable()).getBitmap());

                    container.addView(newView);

                    break;

                case DragEvent.ACTION_DRAG_ENDED:
                    break;

            }

            return true;
        }

When I drop the items stay like this:

inserir a descrição da imagem aqui

Notice that the images are piling up.

  • Whether there are two images or only one?

  • @ramaral I want that when I add, add a copy and this is created where I dropped the finger, like, I dropped it in the middle of the screen, the Imageview is created there, because the way it is, when I release it, they stay there at the top and they pile up :3

  • @ramaral and if possible, that this copy of the added image, it was also possible to drag n drop with it

  • You cannot use a Relativelayout. Use a Absolutelayout or a custom view. In the meantime look around, 'cause there might be something done.

  • @ramaral boy, I got it, but something ready nothing, I’ve already reviewed this whole Internet, I found nothing about

  • @ramaral ready :)

Show 1 more comment

1 answer

1


In my Myondraglistener, I need to specify the position where to put the new version in my layout. Doing so:

 View view = (View) event.getLocalState();
 RelativeLayout container = (RelativeLayout) v;
 RelativeLayout.LayoutParams params;
 params = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
                params.addRule(RelativeLayout.ALIGN_PARENT_LEFT, RelativeLayout.TRUE);
                params.addRule(RelativeLayout.ALIGN_PARENT_TOP, RelativeLayout.TRUE);

ImageView oldView = (ImageView) view;
ImageView newView = new ImageView(getApplicationContext());
params.leftMargin = (int) event.getX() - oldView.getWidth()/2;
params.topMargin = (int) event.getY() - oldView.getHeight()/2;

container.addView(newView, params);

And to add the drag n drop to the new view, just do:

newView.setOnLongClickListener(vi -> { 
ClipData data = ClipData.newPlainText("simple_text", "text"); 
View.DragShadowBuilder shadowBuilder = new View.DragShadowBuilder(vi); 
vi.startDrag(data, shadowBuilder, vi, 0); vi.setVisibility(View.VISIBLE); 
return true; 
});

Answer by @Locdoc01

  • I thought of that possibility but it seemed that it would not work in all situations, as I understood what you intended.

  • 1

    @ramaral worked perfectly, even being inside a scrollView and a Horizontalscrollview

Browser other questions tagged

You are not signed in. Login or sign up in order to post.