A gridview comes handy when organizing the UI content as a two-dimensional scrolling grid. My previous article already shows a simple example of such a control.
To interact with the gridview elements, we might need additional functionality, such as drag and drop capabilities, scrolling, sliding/swiping and so on. This article shows a more complex implementation, that gives you just that and more: now each item view, defined in DynGridViewItemView.java, also shows a top-right image with a dual state, useful to use as a toggle, or as in my example to illustrate the "favorite" attribute of a given item. Drag and drop is intuitive and a short vibration accompanies this action. Here is a demo video of what this code can do:

The drag and drop functionality is the open source code introduced by Google in Android 2.2 for the homescreen launcher drag and drop functionality.
It also defines a DeleteZone, that becomes visible while an item is dragged, and changes color when the item is hovering above, to indicate the option to delete the dragged item, if the user decides so. The DeleteZone extends the ImageView, so the two images for the two states (normal, and target hovering) are introduced with a LevelListDrawable:

  2. mDeleteZone = new DeleteZone(this);
  3. LevelListDrawable a = new LevelListDrawable();
  4. a.addLevel(0, 1, getResources().getDrawable(R.drawable.delete_icon)); // normal image
  5. a.addLevel(1, 2, getResources().getDrawable(R.drawable.delete_icon_red)); // delete image, drag over
  6. mDeleteZone.setImageDrawable(a);

Toggle Scroll/Swipe
I've added a GestureDetector that can be used to detect Swiping/Sliding over the GridView, but this is used in a complementary mode with the default scrolling action: so if you enable swiping, you do it at the cost of disabling the scrolling, and vice-verso. The demo code has a bottom-left button to do just that, also visible in the demo video.
Regardless of the two, the drag and drop functionality is not affected. This has been provided with a complex balance of the touch-based events, as there are three consumers for them: the gridview's default scrolling action, the gridview's drag controller drag and drop functionality and the slider/swide action.
There are custom events generated for the swiping gestures: left, up, down, right.

Toggle Favs
As said, each item can show a custom top-right little image, with two states, useful for showing a toggle icon, and in this case a favorite/unfavorite attribute. This icon can be clicked, and a custom event is generated.

Drag and drop
When an item is dragged, a short vibration is used to signal the beginning of the drag and drop action. Then the source item view is set to Visible.GONE, and the user moves around and overlay, showing a magnified bitmap copy of the original source image. After the drop, the Gridview content is being refreshed: either an item has been deleted, so we refresh to free the space previously occupied, or the item has been dropped over another item, and we need to do the switch.
The dragged item bitmap is shown as a transparent overlay, with a yellowing color, that can be customized in DragView.java's onDraw.

Custom actions
For all your interactions with this GridView, there's an event to help you catch all actions:

  2. public interface DynGridViewListener {
  3. /**
  4. * An item in the DynGridView has been clicked
  5. * @param v the view of this item
  6. * @param position the position in the adapter array
  7. * @param id the item id
  8. */
  9. public void onItemClick(View v, int position, int id);
  10. /**
  11. * TheFAV icon on top right corner of an item in the DynGridView has been clicked
  12. * @param v the view of this item
  13. * @param position the position in the adapter array
  14. * @param id the item id
  15. */
  16. public void onItemFavClick(View v, int positionForView, int id);
  17. /**
  18.   * Dragging has started
  19.   */
  20. public void onDragStart();
  21. /**
  22.   * Dragging has stopped
  23.   */
  24. public void onDragStop();
  25. /**
  26.   * Two items have swapped their positions by drag and drop
  27.   * @param positionOne the first item
  28.   * @param positionTwo the second item
  29.   */
  30. public void onItemsChanged(int positionOne, int positionTwo);
  31. /**
  32.   * This item has been deleted, by dragging over the delete zone
  33.   * @param position the position in the adapter array
  34.   * @param id the item id
  35.   */
  36. public void onItemDeleted(int position, int id);
  38. public void onSwipeLeft();
  40. public void onSwipeRight();
  42. public void onSwipeUp();
  44. public void onSwipeDown();
  45. }

AS you can see this interface covers most of what you can do with this structure, providing easy access to all variables or parameters involved.

A few pictures
android_gridview_drag_and_drop_1 android_gridview_drag_and_drop_2 android_gridview_drag_and_drop_3 android_gridview_drag_and_drop_4
To learn more , see the demo code attached below, released as open source under GPL. For any questions, feel free to contact me, using the comments section at the bottom of this article.

Demo project and open source code
You can download the code here, or on Github.

Related Post