SoFunction
Updated on 2025-04-07

Example of Android method to add drag and drop effect to View

1. Introduction

In development, drag and drop is a common gesture operation. Using it can make the interaction of applications more convenient and friendly. This article will briefly introduce how to add drag and drop effects to Views in Android.

2. Introduction to main methods and classes

2.1 startDragAndDrop() and startDrag()

To implement drag and drop of View, you need to call the startDragAndDrop() or startDrag() method of the View. The startDragAndDrop() method requires the API version to be 24 or above. After calling the method, the View can be dragged. The parameters that need to be passed by this method are as follows:

//data: The data to be passed by drag and drop operation//shadowBuilder: drag and drop shadow//myLocalState: An object containing data related to drag and drop operations//flags: flags that control drag and drop operationpublic final boolean startDragAndDrop(ClipData data, DragShadowBuilder shadowBuilder,Object myLocalState, int flags)

2.2 setOnDragListener()

The view that receives drag and drop events will be called the target View for the time being. The target View calls setOnDragListener() and implements the method onDrag() in it to receive the callback of the drag and drop events.

2.3

When the drag and drop operation is in progress, the dragged image needs to be displayed. The class provides a construction method that can be passed into the View. This View is a drag-and-drop View. We call the drag image created by DragShadowBuilder called the drag shadow. This will be passed as a parameter into the startDragAndDrop() or startDrag() method. If necessary, you can also inherit the class to achieve custom effects.

2.4 DragEvent

This class mainly defines the types of drag and drop events. Different event types can be obtained through (). There are mainly the following types:

//DragEvent.ACTION_DRAG_STARTED: Indicates that the drag has started//DragEvent.ACTION_DRAG_ENTERED: Indicates that the drag shadow has entered the target View//DragEvent.ACTION_DRAG_LOCATION: Drag shadow will respond to this event multiple times when it moves within the target view boundary.//DragEvent.ACTION_DRAG_EXITED: Indicates that the drag shadow has left the boundary of the target view//DragEvent.ACTION_DROP: Indicates that the drag shadow is released//DragEvent.ACTION_DRAG_ENDED:Indicates that the drag and drop operation is about to end,You need to call it here()The return value to determine whether the drag and drop operation is successful

3. Demo drag and drop an image into the box

Now the demonstration will drag and drop an image into the box to illustrate the general process of the drag and drop operation. The box is a LinearLayout, which sets a box background for it.

3.1 Simple layout

The layout is as follows:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:andro
    xmlns:tools="/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <ImageView
        android:
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:layout_centerHorizontal="true"
        android:src="@mipmap/ic_launcher" />

    <LinearLayout
        android:
        android:layout_width="300dp"
        android:layout_height="300dp"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="50dp"
        android:background="@drawable/black_bac"
        android:orientation="vertical">
    </LinearLayout>
</RelativeLayout>

3.2 Operation of drag-and-drop pictures

To realize the effect of long pressing and dragging the picture, first call setOnLongClickListener() to set the long press event callback, then build ClipData and drag shadow, then call startDragAndDrop() or startDrag() method to implement drag. The code is as follows:

iv_drag.setOnLongClickListener(new () {
    @Override
    public boolean onLongClick(View v) {
        CharSequence charSequence = (CharSequence) iv_drag.getTag();
         item = new (charSequence);
        ClipData clipData = new ClipData(charSequence, new String[]{ClipDescription.MIMETYPE_TEXT_PLAIN}, item);
         shadowBuilder = new (iv_drag);
        if (.SDK_INT >= Build.VERSION_CODES.N) {
            iv_drag.startDragAndDrop(clipData, shadowBuilder, null, 0);
        } else {
            iv_drag.startDrag(clipData, shadowBuilder, null, 0);
        }
        return true;
    }
});   

3.3 Box to receive pictures

To receive the image of the target View, you need to call setOnDragListener() to accept the callback of the drag and drop event, use() to obtain different drag and drop event types, and then perform the corresponding operations according to the event type. The example is as follows:

ll_accept.setOnDragListener(new () {
    @Override
    public boolean onDrag(View v, DragEvent event) {
        switch (()) {
            case DragEvent.ACTION_DRAG_STARTED:
                iv_drag.setVisibility();
                if (().hasMimeType(ClipDescription.MIMETYPE_TEXT_PLAIN)) {
                    return true;
                }
                return false;

            case DragEvent.ACTION_DRAG_ENTERED:
                isChangePos = true;
                ll_accept.setBackgroundResource(.green_bac);
                return true;

            case DragEvent.ACTION_DRAG_LOCATION:
                x = ();
                y = ();
                return true;

            case DragEvent.ACTION_DRAG_EXITED:
                isChangePos = false;
                ll_accept.setBackgroundResource(.black_bac);
                return true;

            case DragEvent.ACTION_DROP:

                return true;

            case DragEvent.ACTION_DRAG_ENDED:
                ll_accept.setBackgroundResource(.black_bac);
                if (isChangePos && ()) {
                    int left = ll_accept.getLeft();
                    int top = ll_accept.getTop();
                    x = x + left - (iv_drag.getWidth() / 2);
                    y = y + top - (iv_drag.getHeight() / 2);
                    iv_drag.setX(x);
                    iv_drag.setY(y);
                }
                iv_drag.setVisibility();
                return true;
        }
        return false;
    }
});

4. Summary

During the development process, we will use various Views to achieve drag and drop effects, mainly including drag shadow construction, drag and drop method calls, and handling of drag and drop events. Adding drag and drop effects to the View in a suitable scenario can make application interaction more convenient and friendly.

This is the end of this article about adding drag and drop effects to the View by Android. For more related Android to add drag and drop content to the View, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!