Implementation of Drag-and-Drop gestures using the Drag & amp; Drop framework in Android Development

Source: Internet
Author: User

Drag and Drop gestures in the Drag & Drop framework of Android Development

Android3.0 provides the drag/drop framework, which enables drag-and-drop gestures to drag a view to another view in the current layout. This article describes how to use the drag-and-drop framework.

1. steps for implementing drag and drop

First, let's take a look at the drag-and-drop process. We can see from the official documentation that the entire drag-and-drop process is divided into four steps:

1. Started: Start drag-and-drop. It mainly calls the startDrag method of the drag-and-drop View. This method is prototype:

Public final boolean startDrag (ClipData data,

View. DragShadowBuilder shadowBuilder,

Object myLocalState,

Int flags)

After it is started, the system generates a drag-and-drop shadow and sends the drag-and-drop event with the action ACTION_DRAG_STARTED to the View with the drag-and-drop listener configured in the current layout.

2. Continuing: keep dragging. In this process, the system may send one or more drag events to the View with the drag-and-drop listener set, such as ACTION_DRAG_ENTERED and ACTION_DRAG_LOCATION.

3. Dropped: If you release the drag shadow in the target region, the system will send an action ACTION_DROP event to the View with the drag and drop listener set.

4. Ended: If you release the drag shadow, the system will send the action as ACTION_DRAG_ENDED event to the View with the drag and drop listener set.

2. Key Interfaces and classes in the drag-and-drop process

Secondly, we need to understand several key interfaces and classes in the drag-and-drop process, including OnDragListener, DragEvent, DragShadowBuilder, ClipData, and ClipDescription.

1. OnDragListener: interface, Drag and Drop event listener. When Drag occurs, the method in this interface is called back. The Interface contains only one method onDrag. The method prototype is:

Boolean onDrag (View v, DragEvent event)

Parameter v: sets the View of the listener.

Parameter event: the parameter of the drag-and-drop event, which encapsulates the drag-and-drop data.

Returned value: true-the event has been processed; false: the event has not been processed.

2. DragEvent: Drag and Drop event objects, which contain different event data based on different actions.

3. DragShadowBuilder: The drag-and-drop shadow constructor object used to construct the drag-and-drop shadow.

4. ClipData and ClipDescription: move data by drag and drop.

Iii. case presentation

After having a basic understanding of the above content, let's start with the following case.

There are two areas in the interface, the gray area and the yellow area, the icon can be freely dragged in the gray area, and can change their position, such

When the icon enters or leaves the yellow area, the color is changed as follows:

Iv. Case implementation

Let's take a look at the specific implementation:

  Layout file, the Code is as follows:

 
 
  
   
    
     
    
    
  
 

Java file:

Bind a long click event to the ImageView

 
ImageView. setOnLongClickListener (new OnLongClickListener () {@ Override public boolean onLongClick (View v) {// create ClipData for mobile data. item item = new ClipData. item (String) v. getTag (); ClipData data = new ClipData (IMAGEVIEW_TAG, new String [] {ClipDescription. MIMETYPE_TEXT_PLAIN}, item); // call the startDrag method. The second parameter is to create a drag-and-drop shadow v. startDrag (data, new View. dragShadowBuilder (v), null, 0); return true ;}});

Bind a drag listener to the target View:

 
Container. setOnDragListener (new OnDragListener () {@ Override public boolean onDrag (View v, DragEvent event) {final int action = event. getAction (); switch (action) {case DragEvent. ACTION_DRAG_STARTED: // drag start event if (event. getClipDescription (). hasMimeType (ClipDescription. MIMETYPE_TEXT_PLAIN) {return true;} return false; case DragEvent. ACTION_DRAG_ENTERED: // drag and drop the View to enter the target View container. setBackgroundColor (Color. YELLOW); return true; case DragEvent. ACTION_DRAG_LOCATION: return true; case DragEvent. ACTION_DRAG_EXITED: // drag and drop the View to exit the target View container. setBackgroundColor (Color. BLUE); title. setText (""); return true; case DragEvent. ACTION_DROP: // release the drag-and-drop shadow and obtain the moving data ClipData. item item = event. getClipData (). getItemAt (0); String dragData = item. getText (). toString (); title. setText (dragData + event. getY () + ":" + event. getX (); return true; case DragEvent. ACTION_DRAG_ENDED: // drag and drop events to complete return true; default: break;} return false ;}});
V. Download source code

Click "Download source code" to download the complete project test.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.