Khung kéo và thả của Android cho phép bạn thêm tính năng kéo và thả tương tác vào ứng dụng của mình. Với tính năng kéo và thả, người dùng có thể sao chép hoặc di chuyển văn bản, hình ảnh, đối tượng và mọi nội dung có thể được biểu thị bằng URI, từ một View sang một View khác trong ứng dụng hoặc giữa các ứng dụng ở chế độ nhiều cửa sổ.
Hình 1. Kéo và thả trong ứng dụng.
Hình 2. Kéo và thả giữa các ứng dụng.
Khung này bao gồm một lớp sự kiện kéo, trình nghe kéo, các lớp và phương thức trợ giúp. Mặc dù chủ yếu được thiết kế để cho phép chuyển dữ liệu, nhưng bạn có thể dùng khung này cho các thao tác khác trên giao diện người dùng. Ví dụ: bạn có thể tạo một ứng dụng kết hợp các màu khi người dùng kéo một biểu tượng màu lên trên một biểu tượng khác. Tuy nhiên, phần còn lại của tài liệu mô tả khung kéo và thả trong bối cảnh chuyển dữ liệu.
Tổng quan
Có một vài yếu tố liên quan đến quá trình kéo.
Nguồn kéo: Chế độ xem điểm bắt đầu của quá trình kéo và thả.
Đích thả: Một chế độ xem có thể chấp nhận dữ liệu kéo.
Bóng khi kéo: Bóng khi kéo là hình ảnh thể hiện dữ liệu đang được kéo và người dùng có thể nhìn thấy dữ liệu này.
Sự kiện kéo: Khi người dùng di chuyển bóng khi kéo trên bố cục của ứng dụng, hệ thống sẽ gửi sự kiện kéo đến trình nghe sự kiện kéo và các phương thức gọi lại liên kết với các đối tượng View trong bố cục.
Thao tác kéo và thả bắt đầu khi người dùng thực hiện một cử chỉ trên giao diện người dùng mà ứng dụng của bạn nhận dạng là tín hiệu để bắt đầu kéo dữ liệu. Để phản hồi, ứng dụng sẽ thông báo cho hệ thống rằng thao tác kéo và thả đang bắt đầu. Hệ thống gọi lại ứng dụng của bạn để tạo bóng khi kéo và hiển thị cho người dùng trong quá trình kéo và thả.
Khi người dùng di chuyển bóng khi kéo qua bố cục của ứng dụng, hệ thống sẽ gửi các sự kiện kéo đến trình nghe sự kiện kéo và phương thức gọi lại liên kết với các đối tượng View trong bố cục. Nếu người dùng thả bóng khi kéo trên mục tiêu thả, thì hệ thống sẽ gửi dữ liệu tới đó. Thao tác kéo và thả kết thúc khi người dùng thả bóng khi kéo, cho dù bóng khi kéo có nằm trên mục tiêu thả hay không.
Ngoài ra, hãy triển khai tính năng kéo và thả trong thành phần hiển thị Android, giúp nhà phát triển có nhiều quyền kiểm soát hơn đối với các thông tin chi tiết.
Mẫu kéo và thả chứa nhiều cách khác nhau để triển khai thao tác kéo và thả cùng với việc chấp nhận nội dung đa phương tiện.
Nội dung và mã mẫu trên trang này phải tuân thủ các giấy phép như mô tả trong phần Giấy phép nội dung. Java và OpenJDK là nhãn hiệu hoặc nhãn hiệu đã đăng ký của Oracle và/hoặc đơn vị liên kết của Oracle.
Cập nhật lần gần đây nhất: 2025-07-26 UTC.
[[["Dễ hiểu","easyToUnderstand","thumb-up"],["Giúp tôi giải quyết được vấn đề","solvedMyProblem","thumb-up"],["Khác","otherUp","thumb-up"]],[["Thiếu thông tin tôi cần","missingTheInformationINeed","thumb-down"],["Quá phức tạp/quá nhiều bước","tooComplicatedTooManySteps","thumb-down"],["Đã lỗi thời","outOfDate","thumb-down"],["Vấn đề về bản dịch","translationIssue","thumb-down"],["Vấn đề về mẫu/mã","samplesCodeIssue","thumb-down"],["Khác","otherDown","thumb-down"]],["Cập nhật lần gần đây nhất: 2025-07-26 UTC."],[],[],null,["# Enable drag and drop\n\nTry the Compose way \nJetpack Compose is the recommended UI toolkit for Android. Learn how to use drag and drop in Compose. \n[Drag and drop →](/develop/ui/compose/touch-input/user-interactions/drag-and-drop) \n\n\u003cbr /\u003e\n\n| **Note:** See the improved method of implementing drag and drop in [Receive rich\n| content](/develop/ui/views/receive-rich-content) for a better UX and improved maintainability of your code.\n\nThe Android drag-and-drop framework lets you add interactive drag-and-drop\ncapabilities to your app. With drag and drop, users can copy or move text,\nimages, objects, and any content that can be represented by a URI, from one\n[`View`](/reference/android/view/View) to another within an app, or between apps\nin [multi-window mode](/develop/ui/views/touch-and-input/drag-drop/multi-window#DragPermissionsMultiWindow).\n\n|--------------------------------------------|-------------------------------------------|\n| | |\n| **Figure 1.** Drag and drop within an app. | **Figure 2.** Drag and drop between apps. |\n\nThe framework includes a drag event class, drag listeners, and helper classes\nand methods. Although primarily designed to enable the transfer of data, you can\nuse the framework for other UI actions. For example, you can create an app that\nmixes colors when the user drags a color icon over another icon. However, the\nrest of document describes the drag-and-drop framework in the context of data\ntransfer.\n\nOverview\n--------\n\nThere are a few elements involved in the drag process.\n\n1. Drag source: The start point view of drag-and-drop process.\n\n2. Drop target: A view that can accept the drag data.\n\n3. [Drag shadow](/develop/ui/views/touch-and-input/drag-drop/concepts#AboutDragShadowBuilder):\n A *drag shadow* is a representation\n of the data being dragged, it's visible to users.\n\n4. [Drag events](/develop/ui/views/touch-and-input/drag-drop/concepts#AboutDragEvent):\n As the user moves the drag shadow over the\n app's layout, the system sends drag events to the drag event listeners and\n callback methods associated with the `View` objects in the layout.\n\nA drag-and-drop operation starts when the user makes a UI gesture that your app\nrecognizes as a signal to start dragging data. In\nresponse, the app notifies the system that a drag-and-drop operation is\nstarting. The system calls back to your\napp to get a *drag shadow*. and show it to users during drag-and-drop process.\n\nAs the user moves the drag shadow over the app's layout, the system sends drag\nevents to the [drag event listeners and callback methods](/develop/ui/views/touch-and-input/drag-drop/concepts#AboutDragListeners)\nassociated with the `View` objects in the layout. If the user releases the drag\nshadow over a drop target, the system sends the data to it. The drag-and-drop\noperation ends when the user releases the drag shadow, whether or not the drag\nshadow is over a drop target.\n\nTopics\n------\n\n[Key Concepts](/develop/ui/views/touch-and-input/drag-drop/concepts)\n: Understand the drag-and-drop process.\n\n[DropHelper for simplified drag and drop](/develop/ui/views/touch-and-input/drag-drop/drophelper)\n: Learn how to implement drag and drop with `DropHelper`.\n\n[Implement drag and drop with views](/develop/ui/views/touch-and-input/drag-drop/view)\n: Alternatively, implement drag and drop with Android views, this allows\n developers to have more control of the details.\n\n[Drag and drop in multi-window mode](/develop/ui/views/touch-and-input/drag-drop/multi-window)\n: Support drag and drop in multi-window mode, allow objects to move across\n different applications.\n\nAdditional resources\n--------------------\n\n- [Codelab for Drag and Drop](/codelabs/codelab-dnd-views) using views\n- [Drag \\& drop for seamless multitasking](https://www.youtube.com/watch?v=WOm76wSfkbU) video\n- [Drag and Drop Samples](https://github.com/android/platform-samples/tree/main/samples/user-interface/draganddrop) which contains various ways to implement drag and drop along with accepting rich media."]]