256
CHAPTER 9: User Interface
} else {
theView.startDrag(dragData, shadow,
null, 0)
}
}
true
}
If
the drag operation is not associated with a data type, you can just as well let dragData be
null.
In this case, you don’t have to build a ClipData object.
Listening to Drag Events
The complete set of events occurring during a drag-and-drop operation is governed by a
drag-and-drop listener. Here’s an example:
class MyDragListener : View.OnDragListener {
override
fun onDrag(v: View, event: DragEvent): Boolean {
var res = true
when(event.action) {
DragEvent.ACTION_DRAG_STARTED -> {
when(v.tag) {
"DragSource" -> { res = false
/*not a drop receiver*/ }
"OneTarget" -> {
//
could visibly change
//
possible drop receivers
}
}
}
DragEvent.ACTION_DRAG_ENDED -> {
when(v.tag) {
"OneTarget" -> {
// could visibly change
// possible drop receivers
}
}
}
DragEvent.ACTION_DROP -> {
when(v.tag) {
"OneTarget" -> {
// visually revert drop
// receiver ...
}
}
Toast.makeText(v.context, "dropped!",
Toast.LENGTH_LONG).show()
}