Implementing drag and drop
Here, in the last section of this chapter, we will show you how to implement the drag and drop feature. It's a feature that you will probably need in most applications containing data in lists. Using lists is not mandatory for performing drag and drop, because you can drag anything (view) and release it anywhere where a proper listener is defined. For a better understanding of what we are talking about, we will show you an example of how to implement it.
Let's define a view. On that view, we will set a long press listener that will trigger the drag and drop operation:
view.setOnLongClickListener { val data = ClipData.newPlainText("", "") val shadowBuilder = View.DragShadowBuilder(view) view.startDrag(data, shadowBuilder, view, 0) true }
We used the ClipData
class to pass the data to drop a target. We defined dragListener
like this and assigned it to a view where we expect it to drop:
private val dragListener...