【发布时间】:2021-04-19 08:27:48
【问题描述】:
我在 recyclerView 下有一个 ImageView,在该 imageView 中我通过约束布局放置了一个 imageView(icon)。
如果我现在使用 DragShadowBuilder 将 imageView 拖到 imageView,我想为 imageView 选择的 msg 敬酒。
我已经尝试过这段代码,但它不起作用,并说 Clipdata 标记为空。
请检查下面的代码
@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;
}
【问题讨论】:
-
任何人请帮助...
标签: java android drag-and-drop actionlistener drag