Drag and Drop any object from one view to another.
-
Implement Long Click Listeners
imageView.setOnLongClickListener( v -> { ... })
-
Create
ClipItem
andClipData
object
val clipItem = ClipData.Item(textMessage)
val mimeType = arrayOf(ClipDescription.MIMETYPE_TEXT_PLAIN)
val clipData2 = ClipData(label, mimeType, clipItem)
val clipData = ClipData.newPlainText(label, textMessage)
View.DragShadowBuilder(binding.sivSrc)
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N)
view.startDragAndDrop(clipData, shadowBuilder, null, 0)
else
view.startDrag(clipData, shadowBuilder, null, 0)
Implements drag listener to handle results
view.setOnDragListener { v, event ->
true
}
When user make a UI gesture, app recognizes as a signal to start dragging. Thus,
system invoke drag listeners for us and provide a drag shadow. If dragging is released onto a
receiving view that accepts the data (a drop target), the system will send data to target.
-> Drag Class
-> Drag Listeners
Its constructor is of two types:
-
DragShadowBuilder(View) -> Pass the view to generate the shadow
-
DragShadowBuilder() -> With empty Constuctor either extend
View.DragShadowBuilder
and override its method or you will get an invisible dragging.
There are four basic steps for drag and drop:
1) Started
2) Continuing
3) Dropped
4) Ended