DragStartHelper
class 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);
mDragStartHelper.attach();
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 types |
|---|
interface DragStartHelper.OnDragStartListenerInterface definition for a callback to be invoked when a drag start gesture is detected. |
Public constructors |
|---|
DragStartHelper(Create a DragStartHelper associated with the specified view. |
Public functions |
|
|---|---|
Unit |
attach()Attach the helper to the view. |
Unit |
detach()Detach the helper from the view. |
Unit |
getTouchPosition(point: Point)Compute the position of the touch event that started the drag operation. |
Boolean |
onLongClick(v: View)Handle a long click event. |
Boolean |
onTouch(v: View, event: MotionEvent)Handle a touch event. |
Public constructors
DragStartHelper
DragStartHelper(
view: View,
listener: DragStartHelper.OnDragStartListener
)
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 |
listener for the drag events. |
Public functions
attach
fun attach(): Unit
Attach the helper to the view.
This will replace previously existing touch and long click listeners.
detach
fun detach(): Unit
Detach the helper from the view.
This will reset touch and long click listeners to null.
getTouchPosition
fun getTouchPosition(point: Point): Unit
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
fun onLongClick(v: View): Boolean
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
fun onTouch(v: View, event: MotionEvent): Boolean
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. |