lightbulb_outline Help shape the future of the Google Play Console, Android Studio, and Firebase. Start survey
added in version 24.1.0
belongs to Maven artifact com.android.support:support-compat:28.0.0-alpha1

DragStartHelper

public class DragStartHelper
extends Object

java.lang.Object
   ↳ android.support.v13.view.DragStartHelper


DragStartHelper is a utility class for implementing drag and drop support.

It detects gestures commonly used to start drag (long click for any input source, click and drag for mouse).

It also keeps track of the screen location where the drag started, and helps determining the hot spot position for a drag shadow.

Implement DragStartHelper.OnDragStartListener to start the drag operation:

 DragStartHelper.OnDragStartListener listener = new DragStartHelper.OnDragStartListener {
     protected void onDragStart(View view, DragStartHelper helper) {
         View.DragShadowBuilder shadowBuilder = new View.DragShadowBuilder(view) {
             public void onProvideShadowMetrics(Point shadowSize, Point shadowTouchPoint) {
                 super.onProvideShadowMetrics(shadowSize, shadowTouchPoint);
                 helper.getTouchPosition(shadowTouchPoint);
             }
         };
         view.startDrag(mClipData, shadowBuilder, mLocalState, mDragFlags);
     }
 };
 mDragStartHelper = new DragStartHelper(mDraggableView, listener);
 
Once created, DragStartHelper can be attached to a view (this will replace existing long click and touch listeners):
 mDragStartHelper.attach();
 
It may also be used in combination with existing listeners:
 public boolean onTouch(View view, MotionEvent event) {
     if (mDragStartHelper.onTouch(view, event)) {
         return true;
     }
     return handleTouchEvent(view, event);
 }
 public boolean onLongClick(View view) {
     if (mDragStartHelper.onLongClick(view)) {
         return true;
     }
     return handleLongClickEvent(view);
 }
 

Summary

Nested classes

interface DragStartHelper.OnDragStartListener

Interface definition for a callback to be invoked when a drag start gesture is detected. 

Public constructors

DragStartHelper(View view, DragStartHelper.OnDragStartListener listener)

Create a DragStartHelper associated with the specified view.

Public methods

void attach()

Attach the helper to the view.

void detach()

Detach the helper from the view.

void getTouchPosition(Point point)

Compute the position of the touch event that started the drag operation.

boolean onLongClick(View v)

Handle a long click event.

boolean onTouch(View v, MotionEvent event)

Handle a touch event.

Inherited methods

From class java.lang.Object

Public constructors

DragStartHelper

added in version 24.1.0
DragStartHelper (View view, 
                DragStartHelper.OnDragStartListener listener)

Create a DragStartHelper associated with the specified view. The newly created helper is not initially attached to the view, attach() must be called explicitly.

Parameters
view View: A View

listener DragStartHelper.OnDragStartListener

Public methods

attach

added in version 24.1.0
void attach ()

Attach the helper to the view.

This will replace previously existing touch and long click listeners.

detach

added in version 24.1.0
void detach ()

Detach the helper from the view.

This will reset touch and long click listeners to null.

getTouchPosition

added in version 24.1.0
void getTouchPosition (Point point)

Compute the position of the touch event that started the drag operation.

Parameters
point Point: The position of the touch event that started the drag operation.

onLongClick

added in version 24.1.0
boolean onLongClick (View v)

Handle a long click event.

Parameters
v View: The view that was clicked and held.

Returns
boolean true if the callback consumed the long click, false otherwise.

onTouch

added in version 24.1.0
boolean onTouch (View v, 
                MotionEvent event)

Handle a touch event.

Parameters
v View: The view the touch event has been dispatched to.

event MotionEvent: The MotionEvent object containing full information about the event.

Returns
boolean True if the listener has consumed the event, false otherwise.