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.