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

Source: Internet
Author: User
Tags gety

Design drag-and-drop operations

The main contents of this section are as follows:

1. How to start dragging and dropping;

2. How to respond to events during dragging and dropping;

3. How to respond to falling events;

4. How to end a drag-and-drop operation.

Start dragging

The user starts dragging with a drag gesture, usually by long pressing on the View object. In response, you should do the following things:

1. If necessary, create a clipdata and Clipdata.item object for the data to be moved, and as part of the Clipdata object, save the metadata in the Clipdescription object inside the Clipdata object. Because the drag-and-drop operation does not represent the movement of the data, you can use NULL instead of the actual object.

For example, the following code snippet shows how to create a Clipdata object that contains a ImageView object label on a ImageView object's long-press event.

Create a string for the ImageView label
private static final String Imageview_tag = "icon Bitmap"

Creates a new ImageView
ImageView ImageView = new ImageView (this);

Sets the bitmap for the ImageView from an icon bit map (defined elsewhere)
Imageview.setimagebitmap (MICONBITMAP);

Sets the tag
Imageview.settag (Imageview_tag);

...

Sets a long click Listener for the ImageView using a anonymous listener object that
Implements the Onlongclicklistener interface
Imageview.setonlongclicklistener (Newview.onlongclicklistener () {

Defines the one method for the interface, which was called when the View is long-clicked
Publicboolean Onlongclick (View v) {

Create a new clipdata.
This is the steps to provide clarity. The convenience method
Clipdata.newplaintext () can create a plain text clipdata in one step.

Create a new Clipdata.item from the ImageView object ' s tag
Clipdata.item item= New Clipdata.item (V.gettag ());

Create a new clipdata using the tag as a label, the plain text MIME type, and
The already-created item. This would create a new Clipdescription object within the
Clipdata, and set its MIME type entry to "Text/plain"
Clipdata dragdata= New Clipdata (V.gettag (), Clipdata.mimetype_text_plain,item);

Instantiates the drag Shadow Builder.
View.drawshadowbuilder myshadow= New Mydragshadowbuilder (ImageView);

Starts the drag

V.startdrag (Dragdata,//The data to is dragged
Myshadow,//The Drag Shadow Builder
NULL,//No need to use local data
0//flags (not currently used, set to 0)
);

}

}

2. The following code snippet defines a Mydragshadowbuilder class that creates a small gray rectangle for dragging the TextView object as a drag shadow:

private static class Mydragshadowbuilder extends view.dragshadowbuilder{

The drag shadow image, defined as a drawable thing
Privatestatic drawable Shadow;

Defines the constructor for Mydragshadowbuilder
Publicmydragshadowbuilder (View v) {

Stores the View parameter passed to Mydragshadowbuilder.
Super (v);

Creates a draggable image that would fill the Canvas provided by the system.
Shadow =new colordrawable (Color.ltgray);
}

Defines a callback that sends the drag shadow dimensions and touch point back to the
System.
@Override
Publicvoid onprovideshadowmetrics (Point size,point Touch)
Defines local variables
Privateint width, height;

Sets the width of the shadow to half the width of the original View
width = GetView (). GetWidth ()/2;

Sets the height of the shadow to half the height of the original View
Height = GetView (). GetHeight ()/2;

The drag shadow is a colordrawable. This sets it dimensions to be the same as the
Canvas that the system would provide. As a result, the drag shadow would fill the
Canvas.
Shadow.setbounds (0,0, width, height);

Sets the size parameter ' s width and height values. These get back to the system
Through the size parameter.
Size.set (width, height);

Sets the touch point's position to being in the middle of the drag shadow
Touch.set (WIDTH/2, HEIGHT/2);
}

Defines a callback that draws the drag shadow in a Canvas that the system constructs
From the dimensions passed in Onprovideshadowmetrics ().
@Override
Publicvoid Ondrawshadow (canvas canvas) {

Draws the colordrawable in the "the" the Canvas passed in from the system.
Shadow.draw (canvas);

}

}

Note: You do not have to extend the View.dragshadowbuilder class, because the constructor View.dragshadowbuilder (view) creates a default drag shadow that is the same size as the view object passed to it, and the contact is dragged in the center of the shadow.

Response to a drag start

During a drag-and-drop operation, the system distributes drag events to the drag event listener for the View object in the current layout. The listener should respond to the obtained operation type by calling the Getaction () method. This method returns action_drag_started when the drag is started.

In response to the action_drag_started operation type, the listener should do the following things:

1. Call the Getclipdescription () method to get the Clipdescription object, using the MIME type method in the Clipdescription object to determine if the listener can receive the dragged data.

If the drag operation does not represent the data to be moved, the decision is not necessary.

2. If the listener can accept a drop event, it should return true. This tells the system to continue to send drag events to the listener. If it is not able to receive the drop event, it should return false and the system will no longer send a drag event to the listener.

Note that for action_drag_started events, the following Dragevent object methods cannot obtain valid data: Getclipdata (), GetX (), GetY (), and GetResult ().

Handling events during dragging

A listener that returns true in response to a action_darg_started drag event continues to receive a drag event during a drag. This type of drag-and-drop listener relies on dragging the shadow's position and the visibility of the listener's view object during drag to receive the drag event.

During drag-and-drop, the listener mainly uses a drag-and-drop event to determine if the view object's appearance should be changed.

During dragging, the Getaction method returns one of the following three values:

1. action_drag_entered: When the contact point (the point below the finger on the touch screen) enters the border of the listener View object, the View object's drag listener receives the event.

2. Action_drag_location: Once the drag listener receives a action_drag_entered event, and before the Action_drag_exited event is received, a new Action_drag is received each time the contact is moved _location event. The Getx and Gety () methods return the X-and y-axis coordinates of the contact.

3. action_drag_exited: After dragging the shadow away from the border of the listener View object, the event is sent to the listener that received the Action_drag_entered event before.

The listener does not need to react to these types of operations, and if the listener returns a value to the system, it is ignored. Here are some guidelines for responding to these types of operations:

1. In response to action_drag_entered or action_drag_location events, the listener can change the appearance of the view object to indicate that the view object can accept the drop event.

2. The Action_drag_location event contains data that is valid for the GETX () and Gety () methods, and the return values of the two methods correspond to the position of the contacts. The listener can use this information to modify the appearance of the View object in which the contact is located, or to use this information to determine the exact location of the user's drop shadow.

3. In response to the Action_drag_exited event, the listener should be reset in response to any change in the appearance of the action_drag_entered or Action_drag_location event. This event indicates that the drag-and-drop object has left ready to drop the target.

Responding to drop events

When the user releases a drag shadow on a View object in the app, and the View object is the one that was previously reported to receive the dragged content, the system sends a ACTION_DROP type of drag event to the View object. The listener should do the following things:

1. Call the Getclipdata () method to get the Clipdata object, which is initialized and saved in the drag listener when the StartDrag () method is called. If the drag-and-drop operation does not move the data, then there is no need to do this;

2. Returns true to indicate that the drop event was successfully processed, otherwise false is returned. For the action_drag_ended event, this return value is the value returned by the GetResult () method.

Note that if the system does not send the Action_drop event, the return value for the GetResult () method call to the Action_drag_ended event is false.

The system allows the user to release the drag shadow on top of the view object where the listener does not receive the drag-and-drop event, and allows the user to release the drag shadow in an empty area of the application UI or in an area other than the application, so that the system does not emit an Action_drop type of event, which directly emits a action_ Drag_ended event.

Response Drag End Event

After the user releases the drag shadow, the system immediately sends the action_drag_ended type of drag event to all the drag event listeners in the application, indicating that the drag operation is over.

Each listener should do the following things:

1. If the listener changes the appearance of the view object during operation, the view object should be reset to the default appearance. This is an indication of the end of the operation visible to the user;

2. The listener can optionally call the GetResult () method to find more relevant operations. The GetResult () method also returns true if the listener returns true in response to an event of type Action_drop. In other cases, the GetResult () method returns false, including the absence of a Action_drop event from the system;

3. The listener should return true to the system.

An example of a response to a drag event:

All drag events are received by the callback method or listener of the dragged event. The following code snippet is a simple example of reacting to a drag event in a listener.

Creates a new drag event listener
Mdraglisten =new Mydrageventlistener ();

View imageview= New ImageView (this);

Sets the drag event listener for the View
Imageview.setondraglistener (Mdraglisten);

...

Protectedclass Mydrageventlistener implements view.ondrageventlistener{

This is the method, the system calls when it dispatches a drag event to the
Listener.
Publicboolean Ondrag (View v,dragevent event) {

Defines a variable to store the action type for the incoming event
Finalint action =event.getaction ();

Handles each of the expected events
Switch (action) {

Casedragevent.action_drag_started:

Determines if this View can accept the dragged data
if (Event.getclipdescription (). Hasmimetype (Clipdescription.mimetype_text_plain)) {

As an example of what your application might do,
Applies a blue color tint to the View to indicate that it can accept
Data.
V.setcolorfilter (Color.Blue);

Invalidate the view to force a redraw in the new tint
V.invalidate ();

Returns true to indicate that the View can accept the dragged data.
return (true);

}else {

Returns false. During the current drag and drop operation, this View would
Not receive events again until action_drag_ended is sent.
return (false);

}
Break

casedragevent.action_drag_entered:{

Applies a green tint to the View. Return true; The return value is ignored.

V.setcolorfilter (Color.green);

Invalidate the view to force a redraw in the new tint
V.invalidate ();

return (true);

Break

Casedragevent.action_drag_location:

Ignore the event
return (true);

Break

Casedragevent.action_drag_exited:

Re-sets the color tint to blue. Returns true; The return value is ignored.
V.setcolorfilter (Color.Blue);

Invalidate the view to force a redraw in the new tint
V.invalidate ();

return (true);

Break

Casedragevent.action_drop:

Gets the item containing the dragged data
Clipdata.item item= Event.getclipdata (). Getitemat (0);

Gets the text data from the item.
Dragdata= Item.gettext ();

Displays a message containing the dragged data.
Toast.maketext (This, "dragged data is" + Dragdata, Toast.length_long);

Turns off any color tints
V.clearcolorfilter ();

Invalidates the view to force a redraw
V.invalidate ();

Returns true. Dragevent.getresult () would return true.
return (true);

Break

Casedragevent.action_drag_ended:

Turns off any color tinting
V.clearcolorfilter ();

Invalidates the view to force a redraw
V.invalidate ();

Does a GetResult (), and displays what happened.
if (Event.getresult ()) {
Toast.maketext (This, "the drop is handled.", Toast.length_long);

}else {
Toast.maketext (This, "the drop didn ' t work.", Toast.length_long);

};

Returns true; The value is ignored.
return (true);

Break

An unknown action type is received.
Default
LOG.E ("DragDrop Example", "Unknown action type received by Ondraglistener.");

Break
};

};

};

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

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.