Drag and drop is the most basic gesture operation, the user can click and hold on a picture, text or other data element, then drag it to another application (or other location in the same application) and release the hand, the data can be removed placed in a new location. Gestures typically appear as a long press on a touch screen, or as a click and drag when using a mouse.
△ Simple drag and drop example
While Android has supported drag-and-drop for a long time ( DragEvent was introduced in Android 3.0, or API level 11), implementing full support for handling gestures, events, permissions, and callbacks has proven difficult.
We'll introduce you to the Jetpack DragAndDrop library, currently in Alpha, to help you more easily handle drag and drop data in your app.
/* Copyright 2021 Google LLC.SPDX-License-Identifier: Apache-2.0 */
implementation 'androidx.draganddrop:draganddrop:1.0.0-alpha02'
Drag-and-drop is becoming increasingly important in larger-screen devices: tablets and laptops, especially foldables, are 7 times more likely to use drag-and-drop in split-screen mode than traditional phones. Dragging data from one app to another is a natural experience for users, so users can multitask more efficiently using apps in -screen or mode.
While the platform natively supports dragging text from EditText
, we strongly recommend enabling users to drag any images, files, and text from other components of the app. Just as importantly, we also encourage users to drag and drop data into your app.
△ Drag from one app to another
DropHelper and DragStartHelper together to make it easier to handle gesture support, callbacks, styles, and pixel perfect alignment.
DragStartHelper
DragStartHelper is a tool class in the Jetpack core library, which is usually used to detect gestures to start dragging, such as long press or mouse click-drag operation.
/* Copyright 2021 Google LLC.SPDX-License-Identifier: Apache-2.0 */
// 将视图设置为可拖动以共享文件。
// DragStartHelper 负责拦截拖动手势并设置监听器。
DragStartHelper(draggableView) { view, _ ->
// 自动设置合适的 MIME 类型
val dragClipData = ClipData.newUri(contentResolver, "File", fileUri)
// 设置被拖动对象的视觉效果
// 可以扩展和自定义,我们这里使用默认效果
val dragShadow = View.DragShadowBuilder(view)
// 开始拖动。注意可以使用全局标记实现跨应用拖动。
view.startDragAndDrop(
dragClipData,
dragShadow,
null, // 额外的本地状态信息,可选项
// 由于这是一个 "content:" URI 而不仅仅是纯文本,我们可以使用
// DRAG_FLAG_GLOBAL_URI_READ 标记使得其他应用可以从我们的 ContentProvider
// 中读取信息。如果不使用该标记,其他应用不会收到拖动事件。
DRAG_FLAG_GLOBAL or DRAG_FLAG_GLOBAL_URI_READ)
)
}.attach()
DropHelper
The new DropHelper
is a utility class responsible for listeners and drop targets. Be sure to build DropHelper.Options
with addInnerEditTexts()
to ensure that any nested EditTexts
within your drop target will not gain focus.
/* Copyright 2021 Google LLC.SPDX-License-Identifier: Apache-2.0 */
DropHelper.configureView(
// 处理该放置事件的 Activity
this,
// 目标放置视图会被高亮
outerDropTarget,
// 支持的 MIME 类型
arrayOf(MIMETYPE_TEXT_PLAIN, "image/*"),
// 配置放置目标的选项
DropHelper.Options.Builder()
// 要确保正确高亮放置目标,所有放置目标视图层级内的 EditText 元素
// 必须通过该方法添加到调用中。否则目标视图内的 EditText 而不是目标视图
// 将在拖放操作中获得焦点。
.addInnerEditTexts(innerEditText)
.build()
) { _, payload ->
// 在这里处理数据,返回需要委托给平台的任何内容
...
}
See the 16200a05286b60 Drag and Drop Guide for Android Developers for more details, and Large Screen Example a more in-depth look at DropHelper practices. Welcome to try the alpha version now and look forward to your feedback .
You are welcome click here to submit feedback to us, or share your favorite content and found problems. Your feedback is very important to us, thank you for your support!
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。