我正在使用3.0中引入的拖放API,但是我只想在用户位于屏幕上特定区域时跟随手指拖动。如果他们漂移到该区域之外,我希望影子消失并结束拖动事件。我已经进行了一些搜索,并看到API不支持创建后更新拖动影子,这是我的第一个计划,但是否有任何方法可以在用户实际抬起手指之前停止DragEvent事件?
我正在使用3.0中引入的拖放API,但是我只想在用户位于屏幕上特定区域时跟随手指拖动。如果他们漂移到该区域之外,我希望影子消失并结束拖动事件。我已经进行了一些搜索,并看到API不支持创建后更新拖动影子,这是我的第一个计划,但是否有任何方法可以在用户实际抬起手指之前停止DragEvent事件?
这个问题很老,但由于没有答案; 我想提醒大家,根据官方文档和API here,这现在是默认行为。
可以使用以下引用代码实现此行为:
val imageView = ImageView(this)
// Set the drag event listener for the View.
imageView.setOnDragListener { v, e ->
// Handles each of the expected events.
when (e.action) {
DragEvent.ACTION_DRAG_STARTED -> {
// Determines if this View can accept the dragged data.
if (e.clipDescription.hasMimeType(ClipDescription.MIMETYPE_TEXT_PLAIN)) {
// As an example of what your application might do, applies a blue color tint
// to the View to indicate that it can accept data.
(v as? ImageView)?.setColorFilter(Color.BLUE)
// 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.
true
} else {
// Returns false to indicate that, during the current drag and drop operation,
// this View will not receive events again until ACTION_DRAG_ENDED is sent.
false
}
}
DragEvent.ACTION_DRAG_ENTERED -> {
// Applies a green tint to the View.
(v as? ImageView)?.setColorFilter(Color.GREEN)
// Invalidates the view to force a redraw in the new tint.
v.invalidate()
// Returns true; the value is ignored.
true
}
DragEvent.ACTION_DRAG_LOCATION ->
// Ignore the event.
true
DragEvent.ACTION_DRAG_EXITED -> {
// Resets the color tint to blue.
(v as? ImageView)?.setColorFilter(Color.BLUE)
// Invalidates the view to force a redraw in the new tint.
v.invalidate()
// Returns true; the value is ignored.
true
}
DragEvent.ACTION_DROP -> {
// Gets the item containing the dragged data.
val item: ClipData.Item = e.clipData.getItemAt(0)
// Gets the text data from the item.
val dragData = item.text
// Displays a message containing the dragged data.
Toast.makeText(this, "Dragged data is $dragData", Toast.LENGTH_LONG).show()
// Turns off any color tints.
(v as? ImageView)?.clearColorFilter()
// Invalidates the view to force a redraw.
v.invalidate()
// Returns true. DragEvent.getResult() will return true.
true
}
DragEvent.ACTION_DRAG_ENDED -> {
// Turns off any color tinting.
(v as? ImageView)?.clearColorFilter()
// Invalidates the view to force a redraw.
v.invalidate()
// Does a getResult(), and displays what happened.
when(e.result) {
true ->
Toast.makeText(this, "The drop was handled.", Toast.LENGTH_LONG)
else ->
Toast.makeText(this, "The drop didn't work.", Toast.LENGTH_LONG)
}.show()
// Returns true; the value is ignored.
true
}
else -> {
// An unknown action type was received.
Log.e("DragDrop Example", "Unknown action type received by View.OnDragListener.")
false
}
}
}
View.stopDragAndDrop()
的等效方法(该方法不存在)。因此,问题是:是否可能模仿这样一个想象中的方法,通过它我们可以强制停止拖动过程并停止显示拖影? - Decent Dabblerv
时使用v.clearColorFilter()
和v.invalidate()
吗? - Jagarv.clearColorFilter()
和v.invalidate()
与放置目标有关,而不是所谓的“拖影”。拖影基本上是被拖动的项目的表示,在屏幕上拖动。Panagiotis Giannoutsos似乎提供了一个创建自定义拖影的解决方案,然后通过检查DragEvent
坐标对其进行操作。但我需要深入研究这篇文章,才能完全理解它的可能性。 - Decent Dabbler