I need a if-statement for my drag and drop code:
If the user drag the item into a empty box, all is okay. The item will show in the empty box (this can do my code below).
But, if the user drag the item into a non-empty-box, the item in the non-empty-box will exchange with the drag item. My code drag the item in the box also the box is not empty :-(
And my second Problem: If the user drag the item anywhere on the screen, the item disappear :-(
@Override
public boolean onDrag(View v, DragEvent event) {
switch (event.getAction()) {
case DragEvent.ACTION_DRAG_STARTED:
// Do nothing
break;
case DragEvent.ACTION_DRAG_ENTERED:
v.setBackground(enterShape);
break;
case DragEvent.ACTION_DRAG_EXITED:
v.setBackground(normalShape);
break;
case DragEvent.ACTION_DROP:
// view dropped, reassign the view to the new ViewGroup
View view = (View) event.getLocalState();
ViewGroup owner = (ViewGroup) view.getParent();
owner.removeView(view);
LinearLayout container = (LinearLayout) v;
container.addView(view);
view.setVisibility(View.VISIBLE);
break;
case DragEvent.ACTION_DRAG_ENDED:
v.setBackground(normalShape);
default:
break;
}
return true;
}
Aucun commentaire:
Enregistrer un commentaire