Android--Drag&&drop

Source: Internet
Author: User
Tags gety

ANDROID3.0 provides a Drag/drop framework that allows you to drag and drop a view into another view in the current layout using drag-and-drop.

steps to implement drag and drop

First, let's take a look at the drag-and-drop process, from the official documentation to know that the entire drag-and-drop process is divided into 4 steps, as follows:

1, Started: Start drag and drop, mainly call the StartDrag method that is dragged and dropped view. The prototype for this method is:

 Public Final boolean StartDrag (clipdata data, View.dragshadowbuilder Shadowbuilder, Object mylocalstate,  int flags)

After startup, the system generates drag-and-drop shadows and sends a drag-and-drop event with action action_drag_started to the current layout that has settings for the drag-and-drop listener view.

2, continuing: Keep dragging state. During this process, one or more drag events may be sent to the view where the drag-and-drop listener is set, such as action_drag_entered, Action_drag_location, and so on.

3. Dropped: The user releases the drag shadow within the target area, and the system sends an action-action_drop event to the view with the drag-and-drop listener set.

4, Ended: The user released the Drag shadow, the system will set the drag-and-drop listener view send action as action_drag_ended event, complete the drag and drop.

key interfaces and classes during drag and drop

Secondly, we need to understand clearly several key interfaces and classes in the drag-and-drop process, mainly Ondraglistener, Dragevent, Dragshadowbuilder, Clipdata, clipdescription and so on.

1, Ondraglistener: interface, drag and drop event listener. When drag occurs, the method in this interface is recalled. The interface contains only one method Ondrag, the method prototype is:

Boolean Ondrag (View V, dragevent event) // parameter V: Set the view of the listener // parameter event: Drag-and-drop event parameters that encapsulate drag-and-drop related data // return value: true-event handled; false event unhandled. 

2. Dragevent: Drag-and-drop event object that contains different event data depending on the action.

3. Dragshadowbuilder: Drag and Drop shadow constructor object to construct drag-and-drop shadows.

4, Clipdata, clipdescription: Mobile data for drag and drop.

Code
<?XML version= "1.0" encoding= "Utf-8"?><Relativelayoutxmlns:android= "Http://schemas.android.com/apk/res/android"Android:id= "@+id/topcontainer"Android:layout_width= "Match_parent"Android:layout_height= "Match_parent"android:orientation= "vertical" >    <ImageViewAndroid:id= "@+id/img"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"android:contentdescription= "@null"android:src= "@drawable/ic_launcher" />    <LinearLayoutAndroid:id= "@+id/container"Android:layout_width= "Match_parent"Android:layout_height= "100DP"Android:layout_alignparentbottom= "true"android:orientation= "vertical"Android:background= "#cccccc" >        <TextViewAndroid:id= "@+id/title"Android:layout_width= "Fill_parent"Android:layout_height= "Wrap_content"Android:textcolor= "#ff0000"android:textsize= "18SP" />    </LinearLayout></Relativelayout>

Bind long click events for ImageView

Imageview.setonlongclicklistener (NewOnlongclicklistener () {@Override Public BooleanOnlongclick (View v) {//Create mobile DataClipdata.item Item =NewClipdata.item (String) V.gettag ()); Clipdata Data=NewClipdata (Imageview_tag,Newstring[] {clipdescription.mimetype_text_plain}, item); //Call the StartDrag method, and the second argument is to create a drag-and-drop shadowV.startdrag (data,NewView.dragshadowbuilder (v),NULL, 0); return true; }        });

To target view bindings, drag-and-drop monitoring:

Container.setondraglistener (NewOndraglistener () {@Override Public BooleanOndrag (View V, dragevent event) {Final intAction =event.getaction (); Switch(action) { Casedragevent.action_drag_started://drag and Drop start event                    if(Event.getclipdescription (). Hasmimetype (Clipdescription.mimetype_text_plain)) { return true; }                    return false;  Casedragevent.action_drag_entered://drag and drop view into target viewContainer.setbackgroundcolor (Color.yellow); return true;  Casedragevent.action_drag_location:return true;  Casedragevent.action_drag_exited://drag and drop view away from Target viewContainer.setbackgroundcolor (Color.Blue); Title.settext (""); return true;  CaseDragevent.action_drop://release drag-and-drop shadows and get moving dataClipdata.item Item = Event.getclipdata (). Getitemat (0); String Dragdata=Item.gettext (). toString (); Title.settext (Dragdata+event.gety () + ":" +event.getx ()); return true;  Casedragevent.action_drag_ended://drag-and-drop event completion                    return true; default:                     Break; }                return false; }        });

Location of the ImageView

Topcontainer.setondraglistener (NewOndraglistener () {@Override Public BooleanOndrag (View V, dragevent event) {Final intAction =event.getaction (); Switch(action) { Casedragevent.action_drag_started:return true;  Casedragevent.action_drag_entered:return true;  Casedragevent.action_drag_location:return true;  Casedragevent.action_drag_exited:return true;  CaseDragEvent.ACTION_DROP:imageView.setX (Event.getx ()-imageview.getwidth ()/2); Imageview.sety (Event.gety ()-imageview.getheight ()/2); return true;  Casedragevent.action_drag_ended:return true; default:                     Break; }                return false; }        });
Effect

I'm the dividing line of the king of the Land Tiger.

Reprint: http://www.cnblogs.com/jerehedu/p/4427588.html

Android--Drag&&drop

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.