valexternalAppCallback=remember{object:DragAndDropTarget{overridefunonDrop(event:DragAndDropEvent):Boolean{valpermission=activity.requestDragAndDropPermissions(event.toAndroidDragEvent())// Parse received datapermission?.release()returntrue}}}
Modifier.dragAndDropTarget(shouldStartDragAndDrop={event->
event.mimeTypes().contains(ClipDescription.MIMETYPE_TEXT_PLAIN)},target=callback// or externalAppCallback)
object:DragAndDropTarget{overridefunonStarted(event:DragAndDropEvent){// When the drag event starts}overridefunonEntered(event:DragAndDropEvent){// When the dragged object enters the target surface}overridefunonEnded(event:DragAndDropEvent){// When the drag event stops}overridefunonExited(event:DragAndDropEvent){// When the dragged object exits the target surface}overridefunonDrop(event:DragAndDropEvent):Boolean=true}
[[["容易理解","easyToUnderstand","thumb-up"],["確實解決了我的問題","solvedMyProblem","thumb-up"],["其他","otherUp","thumb-up"]],[["缺少我需要的資訊","missingTheInformationINeed","thumb-down"],["過於複雜/步驟過多","tooComplicatedTooManySteps","thumb-down"],["過時","outOfDate","thumb-down"],["翻譯問題","translationIssue","thumb-down"],["示例/程式碼問題","samplesCodeIssue","thumb-down"],["其他","otherDown","thumb-down"]],["上次更新時間:2025-08-27 (世界標準時間)。"],[],[],null,["Jetpack Compose supports drag and drop with two modifiers:\n\n- [`dragAndDropSource`](/reference/kotlin/androidx/compose/foundation/draganddrop/package-summary#(androidx.compose.ui.Modifier).dragAndDropSource(kotlin.Function1)): Specifies a composable as the starting point of the drag gesture\n- [`dragAndDropTarget`](/reference/kotlin/androidx/compose/foundation/draganddrop/package-summary#(androidx.compose.ui.Modifier).dragAndDropTarget(kotlin.Function1,androidx.compose.ui.draganddrop.DragAndDropTarget)): Specifies a composable that accepts the dropped data\n\nFor example, to enable users to drag an image in your app, create an image\ncomposable and add the `dragAndDropSource` modifier. To set up a drop target,\ncreate another image composable and add the `dragAndDropTarget` modifier.\n\nThe modifiers can be applied to multiple drag sources and multiple drop targets.\n\nThe modifiers enable apps to share data between two or more composables using\n[`ClipData`](/reference/kotlin/android/content/ClipData), which is interoperable with [`View`](/reference/kotlin/android/view/View) implementations.\n| **Note:** Drag and drop is different from [pick up and\n| move](https://m3.material.io/foundations/interaction/gestures#af76950f-a24c-43bd-bfcd-a9eb15768142), a common list pattern.\n\nSpecify a drag source\n\nTo enable drag events inside a component, add the `dragAndDropSource` modifier.\nThis takes a function as a parameter. Inside this function, use\n[`DragAndDropTransferData`](/reference/kotlin/androidx/compose/ui/draganddrop/DragAndDropTransferData) to represent the transferable data. The data can\nbe a remote URI, rich text data on the clipboard, a local file, or more, but\nthey all need to be wrapped in a `ClipData` object. Provide plain text, for\nexample, as follows:\n\n\n```kotlin\nModifier.dragAndDropSource { _ -\u003e\n DragAndDropTransferData(\n ClipData.newPlainText(\n \"image Url\", url\n )\n )\n}https://github.com/android/snippets/blob/5673ffc60b614daf028ee936227128eb8c4f9781/compose/snippets/src/main/java/com/example/compose/snippets/draganddrop/DragAndDropSnippets.kt#L45-L51\n```\n\n\u003cbr /\u003e\n\nTo allow the drag action to cross the borders of the app, the\n`DragAndDropTransferData` constructor accepts a `flags` argument. In the\nfollowing example, the [`DRAG_FLAG_GLOBAL`](/reference/kotlin/android/view/View#drag_flag_global) constant specifies that data can\nbe dragged from one app into another:\n\n\n```kotlin\nModifier.dragAndDropSource { _ -\u003e\n DragAndDropTransferData(\n ClipData.newPlainText(\n \"image Url\", url\n ),\n flags = View.DRAG_FLAG_GLOBAL\n )\n}https://github.com/android/snippets/blob/5673ffc60b614daf028ee936227128eb8c4f9781/compose/snippets/src/main/java/com/example/compose/snippets/draganddrop/DragAndDropSnippets.kt#L55-L62\n```\n\n\u003cbr /\u003e\n\n`DragAndDropTransferData` accepts flags supported by the Android View system.\nSee the list of [View](/reference/kotlin/android/view/View) constants for an exhaustive list of available flags.\n\nReceive drop data\n\nAssign the `dragAndDropTarget` modifier to a composable to enable the composable\nto receive drag-and-drop events. The modifier has two parameters: the first acts\nas a filter and specifies the kind of data the modifier can accept, and the\nsecond delivers the data in a callback.\n\nNote that the callback instance should be *remembered*. The following snippet\nshows how to remember the callback:\n\n\n```kotlin\nval callback = remember {\n object : DragAndDropTarget {\n override fun onDrop(event: DragAndDropEvent): Boolean {\n // Parse received data\n return true\n }\n }\n}https://github.com/android/snippets/blob/5673ffc60b614daf028ee936227128eb8c4f9781/compose/snippets/src/main/java/com/example/compose/snippets/draganddrop/DragAndDropSnippets.kt#L66-L73\n```\n\n\u003cbr /\u003e\n\nTo accept data from other apps, use the [`requestDragAndDropPermission`](/reference/android/app/Activity#requestDragAndDropPermissions(android.view.DragEvent)) to\nenable the reception, and release it once done:\n\n\n```kotlin\nval externalAppCallback = remember {\n object : DragAndDropTarget {\n override fun onDrop(event: DragAndDropEvent): Boolean {\n val permission =\n activity.requestDragAndDropPermissions(event.toAndroidDragEvent())\n // Parse received data\n permission?.release()\n return true\n }\n }\n}https://github.com/android/snippets/blob/5673ffc60b614daf028ee936227128eb8c4f9781/compose/snippets/src/main/java/com/example/compose/snippets/draganddrop/DragAndDropSnippets.kt#L78-L88\n```\n\n\u003cbr /\u003e\n\nIt's important to remember that the `DragAndDropEvent` returned from the Compose\ncallback is different from the one expected by the `requestDragAndDropPermission`\nmethod, so you need to call the [`toAndroidDragEvent()`](/reference/kotlin/androidx/compose/ui/draganddrop/DragAndDropEvent#(androidx.compose.ui.draganddrop.DragAndDropEvent).toAndroidDragEvent()) extension function on the parameter before\npassing it to the permission request.\n\nThe next snippet demonstrates how to handle dropped plain text:\n\n\n```kotlin\nModifier.dragAndDropTarget(\n shouldStartDragAndDrop = { event -\u003e\n event.mimeTypes().contains(ClipDescription.MIMETYPE_TEXT_PLAIN)\n }, target = callback // or externalAppCallback\n)https://github.com/android/snippets/blob/5673ffc60b614daf028ee936227128eb8c4f9781/compose/snippets/src/main/java/com/example/compose/snippets/draganddrop/DragAndDropSnippets.kt#L93-L97\n```\n\n\u003cbr /\u003e\n\nThe callback function should return `true` if the event is consumed, or `false`\nif the event is refused and does not propagate to the parent component.\n\nHandle drag-and-drop events\n\nOverride callbacks in the [`DragAndDropTarget`](/reference/kotlin/androidx/compose/ui/draganddrop/DragAndDropTarget) interface to observe when a\ndrag-and-drop event starts, ends, or enters or exits a component for precise\ncontrol of the UI and the app's behavior:\n\n\n```kotlin\nobject : DragAndDropTarget {\n override fun onStarted(event: DragAndDropEvent) {\n // When the drag event starts\n }\n\n override fun onEntered(event: DragAndDropEvent) {\n // When the dragged object enters the target surface\n }\n\n override fun onEnded(event: DragAndDropEvent) {\n // When the drag event stops\n }\n\n override fun onExited(event: DragAndDropEvent) {\n // When the dragged object exits the target surface\n }\n\n override fun onDrop(event: DragAndDropEvent): Boolean = true\n}https://github.com/android/snippets/blob/5673ffc60b614daf028ee936227128eb8c4f9781/compose/snippets/src/main/java/com/example/compose/snippets/draganddrop/DragAndDropSnippets.kt#L101-L119\n```\n\n\u003cbr /\u003e\n\nAdditional resources\n\nCodelab: [Drag and drop in Compose](/codelabs/codelab-dnd-compose)"]]