Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Using the drag-n-drop framework, instead that cycling the childs and setting the draglistener, I use as a grid item layout container, a DragableLinearLayout that extends the LinearLayout and implements the onDragEvent(DragEvent) method. </p> <p>So you can fill your grid with the adapter as usual and most of the drag and drop code is on the onDragEvent of DragableLinearLayout</p> <pre><code>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; } }; </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload