Drag n Drop - Android

0

I've done a Drag n Drop system, but when I enter the item, it looks like gravity is top, when inserting new ones, they get on top of each other, how do I drop where I drop my finger? / p>

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 do the drop the items look like this:

Realize that images are heaping up.

    
asked by anonymous 23.04.2018 / 03:22

1 answer

1

In my MyOnDragListener, I need to specify the position from which to place 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 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

    
23.04.2018 / 14:46