ViewDragHelper

public class ViewDragHelper
extends Object

java.lang.Object
   ↳ androidx.customview.widget.ViewDragHelper


ViewDragHelper is a utility class for writing custom ViewGroups. It offers a number of useful operations and state tracking for allowing a user to drag and reposition views within their parent ViewGroup.

Summary

Nested classes

class ViewDragHelper.Callback

A Callback is used as a communication channel with the ViewDragHelper back to the parent view using it. 

Constants

int DIRECTION_ALL

Indicates that a check should occur along all axes

int DIRECTION_HORIZONTAL

Indicates that a check should occur along the horizontal axis

int DIRECTION_VERTICAL

Indicates that a check should occur along the vertical axis

int EDGE_ALL

Edge flag set indicating all edges should be affected.

int EDGE_BOTTOM

Edge flag indicating that the bottom edge should be affected.

int EDGE_LEFT

Edge flag indicating that the left edge should be affected.

int EDGE_RIGHT

Edge flag indicating that the right edge should be affected.

int EDGE_TOP

Edge flag indicating that the top edge should be affected.

int INVALID_POINTER

A null/invalid pointer ID.

int STATE_DRAGGING

A view is currently being dragged.

int STATE_IDLE

A view is not currently being dragged or animating as a result of a fling/snap.

int STATE_SETTLING

A view is currently settling into place as a result of a fling or predefined non-interactive motion.

Public methods

void abort()

cancel(), but also abort all motion in progress and snap to the end of any animation.

void cancel()

The result of a call to this method is equivalent to processTouchEvent(android.view.MotionEvent) receiving an ACTION_CANCEL event.

void captureChildView(View childView, int activePointerId)

Capture a specific child view for dragging within the parent.

boolean checkTouchSlop(int directions, int pointerId)

Check if the specified pointer tracked in the current gesture has crossed the required slop threshold.

boolean checkTouchSlop(int directions)

Check if any pointer tracked in the current gesture has crossed the required slop threshold.

boolean continueSettling(boolean deferCallbacks)

Move the captured settling view by the appropriate amount for the current time.

static ViewDragHelper create(ViewGroup forParent, ViewDragHelper.Callback cb)

Factory method to create a new ViewDragHelper.

static ViewDragHelper create(ViewGroup forParent, float sensitivity, ViewDragHelper.Callback cb)

Factory method to create a new ViewDragHelper.

View findTopChildUnder(int x, int y)

Find the topmost child under the given point within the parent view's coordinate system.

void flingCapturedView(int minLeft, int minTop, int maxLeft, int maxTop)

Settle the captured view based on standard free-moving fling behavior.

int getActivePointerId()
View getCapturedView()
int getDefaultEdgeSize()

Return the default size used for edge tracking.

int getEdgeSize()

Return the size of an edge.

float getMinVelocity()

Return the currently configured minimum velocity.

int getTouchSlop()
int getViewDragState()

Retrieve the current drag state of this helper.

boolean isCapturedViewUnder(int x, int y)

Determine if the currently captured view is under the given point in the parent view's coordinate system.

boolean isEdgeTouched(int edges)

Check if any of the edges specified were initially touched in the currently active gesture.

boolean isEdgeTouched(int edges, int pointerId)

Check if any of the edges specified were initially touched by the pointer with the specified ID.

boolean isPointerDown(int pointerId)

Check if the given pointer ID represents a pointer that is currently down (to the best of the ViewDragHelper's knowledge).

boolean isViewUnder(View view, int x, int y)

Determine if the supplied view is under the given point in the parent view's coordinate system.

void processTouchEvent(MotionEvent ev)

Process a touch event received by the parent view.

void setEdgeSize(int size)

Set the range in pixels along the edges of this view that will actively detect edge touches or drags if edge tracking is enabled.

void setEdgeTrackingEnabled(int edgeFlags)

Enable edge tracking for the selected edges of the parent view.

void setMinVelocity(float minVel)

Set the minimum velocity that will be detected as having a magnitude greater than zero in pixels per second.

boolean settleCapturedViewAt(int finalLeft, int finalTop)

Settle the captured view at the given (left, top) position.

boolean shouldInterceptTouchEvent(MotionEvent ev)

Check if this event as provided to the parent view's onInterceptTouchEvent should cause the parent to intercept the touch event stream.

boolean smoothSlideViewTo(View child, int finalLeft, int finalTop)

Animate the view child to the given (left, top) position.

Protected methods

boolean canScroll(View v, boolean checkV, int dx, int dy, int x, int y)

Tests scrollability within child views of v given a delta of dx.

Inherited methods

Constants

DIRECTION_ALL

public static final int DIRECTION_ALL

Indicates that a check should occur along all axes

Constant Value: 3 (0x00000003)

DIRECTION_HORIZONTAL

public static final int DIRECTION_HORIZONTAL

Indicates that a check should occur along the horizontal axis

Constant Value: 1 (0x00000001)

DIRECTION_VERTICAL

public static final int DIRECTION_VERTICAL

Indicates that a check should occur along the vertical axis

Constant Value: 2 (0x00000002)

EDGE_ALL

public static final int EDGE_ALL

Edge flag set indicating all edges should be affected.

Constant Value: 15 (0x0000000f)

EDGE_BOTTOM

public static final int EDGE_BOTTOM

Edge flag indicating that the bottom edge should be affected.

Constant Value: 8 (0x00000008)

EDGE_LEFT

public static final int EDGE_LEFT

Edge flag indicating that the left edge should be affected.

Constant Value: 1 (0x00000001)

EDGE_RIGHT

public static final int EDGE_RIGHT

Edge flag indicating that the right edge should be affected.

Constant Value: 2 (0x00000002)

EDGE_TOP

public static final int EDGE_TOP

Edge flag indicating that the top edge should be affected.

Constant Value: 4 (0x00000004)

INVALID_POINTER

public static final int INVALID_POINTER

A null/invalid pointer ID.

Constant Value: -1 (0xffffffff)

STATE_DRAGGING

public static final int STATE_DRAGGING

A view is currently being dragged. The position is currently changing as a result of user input or simulated user input.

Constant Value: 1 (0x00000001)

STATE_IDLE

public static final int STATE_IDLE

A view is not currently being dragged or animating as a result of a fling/snap.

Constant Value: 0 (0x00000000)

STATE_SETTLING

public static final int STATE_SETTLING

A view is currently settling into place as a result of a fling or predefined non-interactive motion.

Constant Value: 2 (0x00000002)

Public methods

abort

public void abort ()

cancel(), but also abort all motion in progress and snap to the end of any animation.

cancel

public void cancel ()

The result of a call to this method is equivalent to processTouchEvent(android.view.MotionEvent) receiving an ACTION_CANCEL event.

captureChildView

public void captureChildView (View childView, 
                int activePointerId)

Capture a specific child view for dragging within the parent. The callback will be notified but ViewDragHelper.Callback.tryCaptureView(android.view.View, int) will not be asked permission to capture this view.

Parameters
childView View: Child view to capture

activePointerId int: ID of the pointer that is dragging the captured child view

checkTouchSlop

public boolean checkTouchSlop (int directions, 
                int pointerId)

Check if the specified pointer tracked in the current gesture has crossed the required slop threshold.

This depends on internal state populated by shouldInterceptTouchEvent(android.view.MotionEvent) or processTouchEvent(android.view.MotionEvent). You should only rely on the results of this method after all currently available touch data has been provided to one of these two methods.

Parameters
directions int: Combination of direction flags, see DIRECTION_HORIZONTAL, DIRECTION_VERTICAL, DIRECTION_ALL

pointerId int: ID of the pointer to slop check as specified by MotionEvent

Returns
boolean true if the slop threshold has been crossed, false otherwise

checkTouchSlop

public boolean checkTouchSlop (int directions)

Check if any pointer tracked in the current gesture has crossed the required slop threshold.

This depends on internal state populated by shouldInterceptTouchEvent(android.view.MotionEvent) or processTouchEvent(android.view.MotionEvent). You should only rely on the results of this method after all currently available touch data has been provided to one of these two methods.

Parameters
directions int: Combination of direction flags, see DIRECTION_HORIZONTAL, DIRECTION_VERTICAL, DIRECTION_ALL

Returns
boolean true if the slop threshold has been crossed, false otherwise

continueSettling

public boolean continueSettling (boolean deferCallbacks)

Move the captured settling view by the appropriate amount for the current time. If continueSettling returns true, the caller should call it again on the next frame to continue.

Parameters
deferCallbacks boolean: true if state callbacks should be deferred via posted message. Set this to true if you are calling this method from View.computeScroll() or similar methods invoked as part of layout or drawing.

Returns
boolean true if settle is still in progress

create

public static ViewDragHelper create (ViewGroup forParent, 
                ViewDragHelper.Callback cb)

Factory method to create a new ViewDragHelper.

Parameters
forParent ViewGroup: Parent view to monitor

cb ViewDragHelper.Callback: Callback to provide information and receive events

Returns
ViewDragHelper a new ViewDragHelper instance

create

public static ViewDragHelper create (ViewGroup forParent, 
                float sensitivity, 
                ViewDragHelper.Callback cb)

Factory method to create a new ViewDragHelper.

Parameters
forParent ViewGroup: Parent view to monitor

sensitivity float: Multiplier for how sensitive the helper should be about detecting the start of a drag. Larger values are more sensitive. 1.0f is normal.

cb ViewDragHelper.Callback: Callback to provide information and receive events

Returns
ViewDragHelper a new ViewDragHelper instance

findTopChildUnder

public View findTopChildUnder (int x, 
                int y)

Find the topmost child under the given point within the parent view's coordinate system. The child order is determined using ViewDragHelper.Callback.getOrderedChildIndex(int).

Parameters
x int: X position to test in the parent's coordinate system

y int: Y position to test in the parent's coordinate system

Returns
View The topmost child view under (x, y) or null if none found.

flingCapturedView

public void flingCapturedView (int minLeft, 
                int minTop, 
                int maxLeft, 
                int maxTop)

Settle the captured view based on standard free-moving fling behavior. The caller should invoke continueSettling(boolean) on each subsequent frame to continue the motion until it returns false.

Parameters
minLeft int: Minimum X position for the view's left edge

minTop int: Minimum Y position for the view's top edge

maxLeft int: Maximum X position for the view's left edge

maxTop int: Maximum Y position for the view's top edge

getActivePointerId

public int getActivePointerId ()

Returns
int The ID of the pointer currently dragging the captured view, or INVALID_POINTER.

getCapturedView

public View getCapturedView ()

Returns
View The currently captured view, or null if no view has been captured.

getDefaultEdgeSize

public int getDefaultEdgeSize ()

Return the default size used for edge tracking.

Returns
int The default edge size

getEdgeSize

public int getEdgeSize ()

Return the size of an edge. This is the range in pixels along the edges of this view that will actively detect edge touches or drags if edge tracking is enabled.

Returns
int The size of an edge in pixels

getMinVelocity

public float getMinVelocity ()

Return the currently configured minimum velocity. Any flings with a magnitude less than this value in pixels per second. Callback methods accepting a velocity will receive zero as a velocity value if the real detected velocity was below this threshold.

Returns
float the minimum velocity that will be detected

getTouchSlop

public int getTouchSlop ()

Returns
int The minimum distance in pixels that the user must travel to initiate a drag

getViewDragState

public int getViewDragState ()

Retrieve the current drag state of this helper. This will return one of STATE_IDLE, STATE_DRAGGING or STATE_SETTLING.

Returns
int The current drag state

isCapturedViewUnder

public boolean isCapturedViewUnder (int x, 
                int y)

Determine if the currently captured view is under the given point in the parent view's coordinate system. If there is no captured view this method will return false.

Parameters
x int: X position to test in the parent's coordinate system

y int: Y position to test in the parent's coordinate system

Returns
boolean true if the captured view is under the given point, false otherwise

isEdgeTouched

public boolean isEdgeTouched (int edges)

Check if any of the edges specified were initially touched in the currently active gesture. If there is no currently active gesture this method will return false.

Parameters
edges int: Edges to check for an initial edge touch. See EDGE_LEFT, EDGE_TOP, EDGE_RIGHT, EDGE_BOTTOM and EDGE_ALL

Returns