Reputation: 1802
I'm working on a drag and drop application, everything is working fine but I saw an issue. I have 3 ImageViews, two of them are draggable objects and the other one is the drop target. Every time I drop the object in any location other than the drop target, it's totally disappearing!
Below is the code I used:
ImageView iv1, iv2, iv3;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initControls();
}
private void initControls() {
// TODO Auto-generated method stub
iv1 = (ImageView) findViewById (R.id.imageView1);
iv2 = (ImageView) findViewById (R.id.imageView2);
iv1.setTag("a");
iv2.setTag("b");
iv3 = (ImageView) findViewById (R.id.imageView3);
iv1.setOnTouchListener(new MyTouchListener());
iv2.setOnTouchListener(new MyTouchListener());
iv3.setOnDragListener(new MyDragListener());
}
private final class MyTouchListener implements OnTouchListener {
public boolean onTouch(View view, MotionEvent motionEvent) {
if (motionEvent.getAction() == MotionEvent.ACTION_DOWN) {
ClipData data = ClipData.newPlainText("", "");
DragShadowBuilder shadowBuilder = new View.DragShadowBuilder(view);
view.startDrag(data, shadowBuilder, view, 0);
view.setVisibility(View.INVISIBLE);
return true;
} else {
return false;
}
}
}
class MyDragListener implements OnDragListener {
//Drawable enterShape = getResources().getDrawable(R.drawable.shape_droptarget);
//Drawable normalShape = getResources().getDrawable(R.drawable.shape);
@Override
public boolean onDrag(View v, DragEvent event) {
switch (event.getAction()) {
case DragEvent.ACTION_DRAG_LOCATION:
if(v != iv3)
v.setVisibility(View.VISIBLE);
break;
case DragEvent.ACTION_DRAG_STARTED:
// Do nothing
break;
case DragEvent.ACTION_DRAG_ENTERED:
break;
case DragEvent.ACTION_DRAG_EXITED:
break;
case DragEvent.ACTION_DROP:
// Dropped, reassign View to ViewGroup
//handle the dragged view being dropped over a drop view
View view = (View) event.getLocalState();
Drawable dropshadow = getResources().getDrawable(R.drawable.dropshadow);
//view dragged item is being dropped on
ImageView dropTarget = (ImageView) v;
//view being dragged and dropped
ImageView dropped = (ImageView) view;
/*
//if an item has already been dropped here, there will be a tag
Object tag = dropTarget.getTag();
//if there is already an item here, set it back visible in its original place
if(tag!=null)
{
//the tag is the view id already dropped here
int existingID = (Integer)tag;
//set the original view visible again
findViewById(existingID).setVisibility(View.VISIBLE);
}
//set the tag in the target view to the ID of the view being dropped
dropTarget.setTag(dropped.getId());
*/
String temp = "a";
if(temp.equals(view.getTag())){
//dropped.setBackground(dropshadow);
dropTarget.setBackground(dropshadow);
Log.d(temp, "Correct Image");
} else {
Log.d("b", "Wrong Image");
view.setVisibility(View.VISIBLE);
}
break;
case DragEvent.ACTION_DRAG_ENDED:
default:
break;
}
return true;
}
}
What am I missing in here? Any help is truly appreciated. Thanks.
Upvotes: 3
Views: 3820
Reputation: 1802
Finally I solved this myself. I added this method:
private boolean dropEventNotHandled(DragEvent dragEvent) {
return !dragEvent.getResult();
}
and added this:
case DragEvent.ACTION_DRAG_ENDED:
if (dropEventNotHandled(event))
view.setVisibility(View.VISIBLE);
break;
Upvotes: 6
Reputation: 4591
So when you start dragging - motionEvent.getAction() == MotionEvent.ACTION_DOWN
- you execute view.setVisibility(View.INVISIBLE);
But when you stop dragging, you don't do anything to the view. So I guess your view object is still invisible. Add a similar conditional statement to flip the visibility back to visible when the drag finishes.
Off the top of my head, assuming that MotionEvent.ACTION_UP is the end of a drag event, add the following after the first if block in your onTouch method
else if (motionEvent.getAction() == MotionEvent.ACTION_UP) {
view.setVisibility(View.VISIBLE);
return true
}
Keep the final "else" block as well.
Upvotes: 1