How to swap two image views using drag and drop in Android?
One of the great joys of being in Computer Science is learning humility. At least 90% of the time, the answer to the quest: "Why isn't this working?" is that you told the computer to do the wrong thing.
In "case DragEvent.ACTION_DROP:" I was getting references to both views. What I wasn't realizing for some reason is that when I did
target.setImageDrawable(dragged.getDrawable());
I was also changing the drawable of temp, since temp was referring to the actual view. Because of that when I did
dragged.setImageDrawable(temp.getDrawable());
I was actually just setting dragged's drawable to what it originally was. Here is the corrected code.
case DragEvent.ACTION_DROP:
ImageView target = (ImageView) v;
ImageView dragged = (ImageView) event.getLocalState();
Drawable target_draw = target.getDrawable();
Drawable dragged_draw = dragged.getDrawable();
dragged.setImageDrawable(target_draw);
target.setImageDrawable(dragged_draw);
break;