If you need to support gingerbread you can take a look at my example here
https://github.com/NAYOSO/android-dragview
if you only need to support jelly bean above you can use the Drag and Drop from android library you can see it from this article
http://developer.android.com/guide/topics/ui/drag-drop.html
For some explanation about the Drag and Drop view
at first you need t create the touch listener and then call startDrag to start draging. As simple as that.
private final class dragTouchListener implements OnTouchListener {
@Override
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
DragShadowBuilder shadowBuilder = new View.DragShadowBuilder(v);
v.startDrag(data, shadowBuilder, v, 0);
return true;
} else {
return false;
}
}
}
To monitor the target of dropping place you can use onDragListener
private class dropListener implements OnDragListener {
View draggedView;
CustomTextView dropped;
@Override
public boolean onDrag(View v, DragEvent event) {
switch (event.getAction()) {
case DragEvent.ACTION_DRAG_STARTED:
draggedView = (View) event.getLocalState();
dropped = (CustomTextView) draggedView;
draggedView.setVisibility(View.INVISIBLE);
break;
case DragEvent.ACTION_DRAG_ENTERED:
break;
case DragEvent.ACTION_DRAG_EXITED:
break;
case DragEvent.ACTION_DROP:
CustomTextView dropTarget = (CustomTextView) v;
dropTarget.setText(dropped.getText().toString());
break;
case DragEvent.ACTION_DRAG_ENDED:
break;
default:
break;
}
return true;
}
}
As you can see from my code there is many event but the main one is when the view is start being dragged, dropped and ended.
Don't forget to set the listener to view
tvDrag.setOnTouchListener(new dragTouchListener());
tvDrop.setOnDragListener(new dropListener())
I hope my explanation is clear enough!
If you have further question I will try to answer it tonight or tomorrow :)
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…