Android User Interface---Drag and drop (Drag and Drop) (ii)

Source: Internet
Author: User
Tags event listener

Drag and drop event listeners and callback methods

The View object can be used either to implement a drag-and-drop event listener that implements the View.ondraglistener interface, or to receive a drag event using the Ondragevent (Dragevent) callback method of the View object. When the system calls this callback method or listener, it passes a dragevent object to them.

In most scenarios, you might use listeners. Because there is usually no subclass of the view class when designing the UI interface, and the callback method is used to override this method, you are forced to use the view class and subclasses. By contrast, you can implement a listener class and then use it in several different view objects. You can also implement the listener interface as an anonymous inner class. Call the Setondraglistener () method to set the listener for the view object.

The view object can have both a listener and a callback method, and if this happens, the listener is called first. The system does not invoke a callback method unless the listener returns FALSE.

The combination of the Ondragevent (Dragevent) callback method and the View.ondraglistener listener is followed by the Ontouchevent () callback method for the touchscreen event and The View.ontouchlistener listener is similar.

Drag and Drop events

The system uses the Dragevent object as a drag event. This object contains an action type that tells the listener what happened during the drag-and-drop process. This object also contains additional data, depending on the type of operation.

Call the getaction () method to get the operation type. There are six possible values that are defined as constants in the Dragevent class. Details are shown in table 1 below.

The Dragevent object also contains data that the application provides to the system when it calls the StartDrag () method. Some of these data are only valid for specific types of operations. The valid data that can be obtained for each type of operation is outlined in table 2 below.

Table 1.DragEvent operation Types

Getaction () method return value

Meaning

action_drag_started

The drag event Listener of the View object receives this event action only when the application calls the StartDrag () method and the drag shadow is obtained.

Action_drag_entered

The drag event Listener of the View object receives this type of event action when the drop shadow has just entered the border of the View object.

Action_drag_location

when the View object receives a action_drag_entered event, and the drag shadow is still within the bounds of the object, the drag event listener of the View object receives this type of event action

action_drag_exited

After the view object receives a action_drag_entered and at least one action_drag_location event, the object's event listener accepts this type of operation.

action_drop

true, then this type of operation will only be sent to a view object. If the user is view object, or the user does not release the Operation shadow on any part of the current layout, the operation type will not be sent. If view object successfully handles drop event, listener to return true, otherwise it should return false.

action_drag_ended

When the system ends a drag-and-drop operation, theview object Drag listener receives this type of event action. This type of operation is not necessarily a action_drop event. If the system sends a Action_drop event, then receiving the action_drag_ended operation type does not mean that the drop operation was successful. The listener must call the GetResult () method to get the return value in response to the Action_drop event. If the Action_drop event is not sent, then GetResult () returns false.

2. Get a valid dragevent Object data, x represents the ability to obtain valid data.

getaction () Value

getclipdescription ()

getlocalstate ()

getx ()

getclipdata ()

Span style= "color: #333333;" >getresult ()

action_drag_started

x

x

x

x

 

 

action_drag_entered

x

x

 

 

 

 

action_drag_location

x

x

x

x

 

 

action_drag_exited

x

< Span style= "color: #333333;" >x

&NBSP;

&NBSP;

&NBSP;

&NBSP;

action_drop

x

x

x

x

&NBSP;

action_drag_ended

X

X

X

The Getaction (),describecontents (),Writetoparcel (), and ToString methods always return valid data.

For a particular type of operation, if a method does not contain valid data, it will return null or 0 depending on the type .

dragging shadows

During drag and drop operations, a picture that the user dragged is displayed. For the data to be moved, this image represents the data being dragged. For operations, this picture represents some of the appearance of a drag operation.

This image is called a drag shadow, uses the View.dragshadowbuilder object method to create it, and passes the object to the system when the StartDrag () method is used to start dragging. As part of the response StartDrag () method, the system invokes the callback method defined in the View.dragshadowbuilder object to get the drag shadow.

The View.dragshadowbuilder class has two constructors:

View.dragshadowbuilder (View):

This constructor receives the view object for any application . This constructor saves the view object in the View.dragshadowbuilder object so that the View object is accessed during the callback to construct the drag shadow. It (the View object parameter) does not have to be associated with the view object that the user chooses to start the drag operation .

If you use this constructor, you do not have to extend the View.dragshadowbuilder class or overwrite its method. By default, you get a drag shadow that looks the same as the view object passed to the constructor. At the bottom of the user's touch screen, the out-of-focus display.

View.dragshadowbuilder ():

If you use this constructor, there is no valid view object in the Viewdragshadowbuilder object . By default, if you use this constructor and do not extend the View.dragshadowbuilder class or overwrite it, you will get an invisible drag shadow that will not give you an error.

There are two methods of the Viewdragshadowbuilder class:

Onprovideshadowmetrics ():

After you call the StartDrag () method, the system immediately calls this method, sending the system the size and contact of the drop shadow. This method has two parameters:

Dimensions: A point object where x represents the width of the shadow andy represents the height of the shadow;

Touch_point: A point object, which should be the position of the dragged shadow below the user's finger during the drag,x for the x-axis coordinates, andy for the y-axis.

Ondrawshadow ():

after calling the Onprovidershadowmetrics () callback, the system immediately calls the Ondrawshadow () method to get the drag shadow. This method has a canvas parameter (the canvasobject), and the system uses the parameters provided in the Onprovideshadowmetrics () method to construct the canvas object and draw a drop shadow in the object.

To improve performance, it is necessary to keep the drop shadow in a small size. For a single item, you can use an icon for multiple selections, which can be an icon in the stack instead of a full picture scattered across the screen.

Android User Interface---Drag and drop (Drag and Drop) (ii)

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.