Using the Android drag-and-drop framework, you can allow users to move data from one view object in the current layout to another view object using a graphical drag-and-drop gesture . This framework includes: drag-and-drop event classes, drag-and-drop listeners, and helper methods and classes.
Although this framework is primarily designed for data movement, you can use it for other UI operations. For example, you can create a palette application that allows the user to drag a color icon onto another color icon to complete the two-color leveling operation.
Profile
A drag-and-drop operation begins when the user begins to drag the data using some recognized gesture signals. In the response, the application tells the system that the drag is starting. The system will callback the application to get a graphic representing the dragged data, and when the user's finger moves over the current layout representing the graphic (dragging the shadow), the drag event is sent to the drag event listener object, and the drag event callback method is associated with the corresponding view object in the layout . Once the user releases the drag shadow graphic, the drag operation is ended.
Create a Drag event listener object from the class that implements the View.ondraglistener interface. Use the Setondraglistener () method of the View object to set the drag event listener object to a view object. Each View object also has a ondragevent () callback method. These two methods are described in detail in the "drag-and-drop event listener and callback method" section.
Note: for the sake of simplicity, the chapter uses "Drag-and-drop event listener" as a sample program to receive drag events, although the callback method can also be used in practice.
At the beginning of the drag, the data to be moved and the metadata describing the data as part of the system call. During drag-and-drop, the system sends a drag event to the callback method of each View object in the drag-and-drop event listener or layout. Listeners or callback methods can use metadata to determine whether they can accept this data. If the user drops the data on top of a View object, and the listener or callback method for the View object has told the system that it wants to receive the data, the system sends the data to the listener or callback method in the drag event.
By calling the StartDrag () method, the application tells the system to start the drag action. This tells the system to start sending drag events. This method also sends the data being dragged and dropped.
You can call the StartDrag () method on any view object that is bound in the current layout. The system uses only the view object to gain access to the global settings in the layout.
Once your application calls the StartDrag () method, the rest of the process is to use the event that the system sends to the view object in the layout .
Drag-and-drop process
There are four basic steps or statuses in the drag-and-drop process:
1. Start
To start dragging in response to a user's screen gesture, the application calls the StartDrag () method to tell the system to start dragging. the parameters provided to the StartDrag () method include: Dragged data, metadata for the data, and a callback method that depicts the drop shadow.
The system responds to this call by first returning the drop shadow that the application needs. The shadow is then displayed on the device.
Next, the system sends a drag event with the action_drag_started type to the drag event listener for all view objects in the current layout , and if you want to continue receiving drag events including possible drop events, The drag event listener for the View object must return true. This registers the listener in the system. Only registered listeners can continue to receive drag-and-drop events. At this point, the listener can also change the appearance of its view object to indicate that the listener of the object can receive the drop event.
If the drag event listener returns false, the event listener will not receive a drag event for the current operation until the system sends a drag event with the action_drag_ended type operation. By sending a false return value, the listener tells the system that it is not interested in the drag operation and does not receive the dragged data.
2. Continuous
During user drag and drop, when the drop shadow intersects the border of a view object, the system sends one or more drag events to the View object's Drag event listener (if it is registered to receive these events). The listener can also choose to change the appearance of the view object that responds to the event . For example, if the event indicates that a drag shadow has entered the bounding rectangle of the view object that accepts the drag event , the listener can react by highlighting its view object.
3. put down
The user releases the drop shadow within the bounding rectangle of the view object that can accept the data , and the system sends a drag event with the Action_drop action type to the listener of the View object. This drag event contains the data passed to the system in the StartDrag () method call. If accepted successfully, the listener returns true to the system.
Note that this step occurs only if the user drops the drop Shadow View object (the object is registered to accept the drag event), and if the user releases the drag shadow in any other place that does not receive the drag event, there will be no action_drop drag event.
4. End
After the user has released the drop shadow and, if necessary, a drag event with the Action_drop action type is issued, the system emits a drag event with the type of action_drag_ended action, indicating that the drag operation is over. The time the user releases the Drop shadow is the time to cancel the registration. This event is sent to each listener registered to accept the drag event, even if the listener receives the Action_drop event.
Android User Interface---Drag and drop (Drag and Drop) (i)