Используя каркас drag-n-drop, вместо циклического переключения дочерних объектов и установки draglistener, я использую в качестве контейнера макета элемента сетки DragableLinearLayout, который расширяет LinearLayout и реализует метод onDragEvent (DragEvent).
Таким образом, вы можете заполнить свою сетку с помощью адаптера как обычно, и большая часть кода перетаскивания находится в onDragEvent of DragableLinearLayout
public class DragableLinearLayout extends LinearLayout {
public DragableLinearLayout(Context context, AttributeSet attrs,
int defStyle) {
super(context, attrs, defStyle);
}
public DragableLinearLayout(Context context, AttributeSet attrs) {
super(context, attrs);
}
public DragableLinearLayout(Context context) {
super(context);
}
@Override
public boolean onDragEvent(DragEvent event) {
//in wich grid item am I?
GridView parent = (GridView) getParent();
Object item = parent.getAdapter().getItem(
parent.getPositionForView(this));
//if you need the database id of your item...
Cursor cur = (Cursor) item;
long l_id = cur.getLong(cur.getColumnIndex("youritemid"));
switch (event.getAction()) {
case DragEvent.ACTION_DRAG_STARTED:
return true;
case DragEvent.ACTION_DRAG_ENTERED:
setBackgroundColor(Color.GREEN);
invalidate();
return true;
case DragEvent.ACTION_DRAG_EXITED:
setBackgroundColor(Color.WHITE);
invalidate();
return false;
case DragEvent.ACTION_DROP:
ClipData cd = event.getClipData();
long l_id_start = Long.valueOf(cd.getItemAt(0).getText()
.toString());
//
Toast.makeText(getContext(), "DROP FROM " + l_id_start
+ " TO " + l_id, Toast.LENGTH_LONG);
//do your stuff
........
//the db requery will be on the onDragEvent.drop of the container
//see the listener
return false;
case DragEvent.ACTION_DRAG_ENDED:
setBackgroundColor(Color.WHITE);
invalidate();
//
return false;
}
return true;
}
}
private View.OnDragListener listenerOnDragEvent = new View.OnDragListener() {
public boolean onDrag(View v, DragEvent event) {
// Defines a variable to store the action type for the incoming
// event
final int action = event.getAction();
switch (action) {
case DragEvent.ACTION_DROP:
// REQUERY
updateDbView();
return false;
// break;
}
return true;
}
};