Java 如何在android studio中设置ImageView的拖放?
我在recyclerView下有一个ImageView,在该ImageView中,我通过约束布局放置了一个ImageView(图标) 如果我现在使用DragShadowBuilder将imageView拖到imageView中,我想发送imageView选择的消息。 我已经尝试过这段代码,但它不起作用,并且说Clipdata标记为null 请检查下面的代码Java 如何在android studio中设置ImageView的拖放?,java,android,drag-and-drop,actionlistener,drag,Java,Android,Drag And Drop,Actionlistener,Drag,我在recyclerView下有一个ImageView,在该ImageView中,我通过约束布局放置了一个ImageView(图标) 如果我现在使用DragShadowBuilder将imageView拖到imageView中,我想发送imageView选择的消息。 我已经尝试过这段代码,但它不起作用,并且说Clipdata标记为null 请检查下面的代码 @Override public boolean onDrag(View v, DragEvent event) {
@Override
public boolean onDrag(View v, DragEvent event)
{
Log.d(TAG, "onDrag");
// Store the action type for the incoming event
final int action = event.getAction();
// Handles each of the expected events
switch (action)
{
case DragEvent.ACTION_DRAG_STARTED:
// In order to inform user that drag has started, we apply yellow tint to view
((ImageView) v).setColorFilter(Color.YELLOW);
// Invalidate the view to force a redraw in the new tint
v.invalidate();
// Returns true to indicate that the View can accept the
// dragged data.
return true;
case DragEvent.ACTION_DRAG_ENTERED:
// Apply a gray tint to the View
((ImageView) v).setColorFilter(Color.LTGRAY);
// Invalidate the view to force a redraw in the new tint
v.invalidate();
return true;
case DragEvent.ACTION_DRAG_LOCATION:
// Ignore the event
return true;
case DragEvent.ACTION_DRAG_EXITED:
// Re-sets the color tint to yellow
((ImageView) v).setColorFilter(Color.YELLOW);
// Invalidate the view to force a redraw in the new tint
v.invalidate();
return true;
case DragEvent.ACTION_DROP:
save_drag.setTag("bookmark");
// Create a new ClipData.
// This is done in two steps to provide clarity. The convenience method
// ClipData.newPlainText() can create a plain text ClipData in one step.
// Create a new ClipData.Item from the ImageView object's tag
ClipData.Item item = new ClipData.Item(v.getTag());
// Create a new ClipData using the tag as a label, the plain text MIME type, and
// the already-created item. This will create a new ClipDescription object within the
// ClipData, and set its MIME type entry to "text/plain"
ClipData dragData = new ClipData(v.getTag(),ClipData.MIMETYPE_TEXT_PLAIN,item);
// Gets the item containing the dragged data
ClipData dragData = event.getClipData();
// Gets the text data from the item.
final String tag = dragData.getItemAt(0).toString();
// Displays a message containing the dragged data.
Toast.makeText(mContext, "The dragged image is " + tag, Toast.LENGTH_SHORT).show();
// Turns off any color tints
((ImageView) v).clearColorFilter();
// Invalidates the view to force a redraw
v.invalidate();
return true;
case DragEvent.ACTION_DRAG_ENDED:
// Turns off any color tinting
((ImageView) v).clearColorFilter();
// Invalidates the view to force a redraw
v.invalidate();
// Check for result
if (event.getResult())
{
Toast.makeText(mContext, "Bingo !", Toast.LENGTH_SHORT).show();
}
else
{
Toast.makeText(mContext, "Oups, try again !", Toast.LENGTH_SHORT).show();
save_drag.setImageBitmap(null);
}
return true;
default:
break;
}
return false;
}
任何人都可以帮忙。。。