-
public class 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.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description public abstract class
ViewDragHelper.Callback
A Callback is used as a communication channel with the ViewDragHelper back to theparent view using it.
on*
methods are invoked on siginficant events and severalaccessor methods are expected to provide the ViewDragHelper with more informationabout the state of the parent view upon request. The callback also makes decisionsgoverning the range and draggability of child views.
-
Field Summary
Fields Modifier and Type Field Description public final static int
INVALID_POINTER
public final static int
STATE_IDLE
public final static int
STATE_DRAGGING
public final static int
STATE_SETTLING
public final static int
EDGE_LEFT
public final static int
EDGE_RIGHT
public final static int
EDGE_TOP
public final static int
EDGE_BOTTOM
public final static int
EDGE_ALL
public final static int
DIRECTION_HORIZONTAL
public final static int
DIRECTION_VERTICAL
public final static int
DIRECTION_ALL
-
Method Summary
Modifier and Type Method Description static ViewDragHelper
create(@NonNull() ViewGroup forParent, @NonNull() ViewDragHelper.Callback cb)
Factory method to create a new ViewDragHelper. static ViewDragHelper
create(@NonNull() ViewGroup forParent, float sensitivity, @NonNull() ViewDragHelper.Callback cb)
Factory method to create a new ViewDragHelper. void
setMinVelocity(float minVel)
Set the minimum velocity that will be detected as having a magnitude greater than zeroin pixels per second. float
getMinVelocity()
Return the currently configured minimum velocity. int
getViewDragState()
Retrieve the current drag state of this helper. void
setEdgeTrackingEnabled(int edgeFlags)
Enable edge tracking for the selected edges of the parent view. int
getEdgeSize()
Return the size of an edge. void
setEdgeSize(@Px() int size)
Set the range in pixels along the edges of this view that will activelydetect edge touches or drags if edge tracking is enabled. int
getDefaultEdgeSize()
Return the default size used for edge tracking. void
captureChildView(@NonNull() View childView, int activePointerId)
Capture a specific child view for dragging within the parent. View
getCapturedView()
int
getActivePointerId()
int
getTouchSlop()
void
cancel()
The result of a call to this method is equivalent to processTouchEvent receiving an ACTION_CANCEL event. void
abort()
cancel, but also abort all motion in progress and snap to the end of anyanimation. boolean
smoothSlideViewTo(@NonNull() View child, int finalLeft, int finalTop)
Animate the view child
to the given (left, top) position.boolean
settleCapturedViewAt(int finalLeft, int finalTop)
Settle the captured view at the given (left, top) position.The appropriate velocity from prior motion will be taken into account. void
flingCapturedView(int minLeft, int minTop, int maxLeft, int maxTop)
Settle the captured view based on standard free-moving fling behavior. boolean
continueSettling(boolean deferCallbacks)
Move the captured settling view by the appropriate amount for the current time. boolean
isPointerDown(int pointerId)
Check if the given pointer ID represents a pointer that is currently down (to the bestof the ViewDragHelper's knowledge). boolean
shouldInterceptTouchEvent(@NonNull() MotionEvent ev)
Check if this event as provided to the parent view's onInterceptTouchEvent shouldcause the parent to intercept the touch event stream. void
processTouchEvent(@NonNull() MotionEvent ev)
Process a touch event received by the parent view. boolean
checkTouchSlop(int directions)
Check if any pointer tracked in the current gesture has crossedthe required slop threshold. boolean
checkTouchSlop(int directions, int pointerId)
Check if the specified pointer tracked in the current gesture has crossedthe required slop threshold. 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. boolean
isEdgeTouched(int edges, int pointerId)
Check if any of the edges specified were initially touched by the pointer withthe specified ID. boolean
isCapturedViewUnder(int x, int y)
Determine if the currently captured view is under the given point in theparent view's coordinate system. boolean
isViewUnder(@Nullable() View view, int x, int y)
Determine if the supplied view is under the given point in theparent view's coordinate system. View
findTopChildUnder(int x, int y)
Find the topmost child under the given point within the parent view's coordinate system. -
-
Method Detail
-
create
static ViewDragHelper create(@NonNull() ViewGroup forParent, @NonNull() ViewDragHelper.Callback cb)
Factory method to create a new ViewDragHelper.
- Parameters:
forParent
- Parent view to monitorcb
- Callback to provide information and receive events
-
create
static ViewDragHelper create(@NonNull() ViewGroup forParent, float sensitivity, @NonNull() ViewDragHelper.Callback cb)
Factory method to create a new ViewDragHelper.
- Parameters:
forParent
- Parent view to monitorsensitivity
- Multiplier for how sensitive the helper should be about detectingthe start of a drag.cb
- Callback to provide information and receive events
-
setMinVelocity
void setMinVelocity(float minVel)
Set the minimum velocity that will be detected as having a magnitude greater than zeroin pixels per second. Callback methods accepting a velocity will be clamped appropriately.
- Parameters:
minVel
- Minimum velocity to detect
-
getMinVelocity
float getMinVelocity()
Return the currently configured minimum velocity. Any flings with a magnitude lessthan this value in pixels per second. Callback methods accepting a velocity will receivezero as a velocity value if the real detected velocity was below this threshold.
-
getViewDragState
int getViewDragState()
Retrieve the current drag state of this helper. This will return one of STATE_IDLE, STATE_DRAGGING or STATE_SETTLING.
-
setEdgeTrackingEnabled
void setEdgeTrackingEnabled(int edgeFlags)
Enable edge tracking for the selected edges of the parent view.The callback's onEdgeTouched and onEdgeDragStarted methods will only be invokedfor edges for which edge tracking has been enabled.
- Parameters:
edgeFlags
- Combination of edge flags describing the edges to watch
-
getEdgeSize
@Px() int getEdgeSize()
Return the size of an edge. This is the range in pixels along the edges of this viewthat will actively detect edge touches or drags if edge tracking is enabled.
-
setEdgeSize
void setEdgeSize(@Px() int size)
Set the range in pixels along the edges of this view that will activelydetect edge touches or drags if edge tracking is enabled.
- Parameters:
size
- Edge size in pixels
-
getDefaultEdgeSize
@Px() int getDefaultEdgeSize()
Return the default size used for edge tracking.
-
captureChildView
void captureChildView(@NonNull() View childView, int activePointerId)
Capture a specific child view for dragging within the parent. The callback will be notifiedbut tryCaptureView will not be asked permission tocapture this view.
- Parameters:
childView
- Child view to captureactivePointerId
- ID of the pointer that is dragging the captured child view
-
getCapturedView
@Nullable() View getCapturedView()
-
getActivePointerId
int getActivePointerId()
-
getTouchSlop
@Px() int getTouchSlop()
-
cancel
void cancel()
The result of a call to this method is equivalent to processTouchEvent receiving an ACTION_CANCEL event.
-
abort
void abort()
cancel, but also abort all motion in progress and snap to the end of anyanimation.
-
smoothSlideViewTo
boolean smoothSlideViewTo(@NonNull() View child, int finalLeft, int finalTop)
Animate the view
child
to the given (left, top) position.If this method returns true, the caller should invoke continueSettling on each subsequent frame to continue the motion until it returns false. If this methodreturns false there is no further work to do to complete the movement.This operation does not count as a capture event, though getCapturedView will still report the sliding view while the slide is in progress.
- Parameters:
child
- Child view to capture and animatefinalLeft
- Final left position of childfinalTop
- Final top position of child
-
settleCapturedViewAt
boolean settleCapturedViewAt(int finalLeft, int finalTop)
Settle the captured view at the given (left, top) position.The appropriate velocity from prior motion will be taken into account.If this method returns true, the caller should invoke continueSettling on each subsequent frame to continue the motion until it returns false. If this methodreturns false there is no further work to do to complete the movement.
- Parameters:
finalLeft
- Settled left edge position for the captured viewfinalTop
- Settled top edge position for the captured view
-
flingCapturedView
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 on each subsequent frameto continue the motion until it returns false.
- Parameters:
minLeft
- Minimum X position for the view's left edgeminTop
- Minimum Y position for the view's top edgemaxLeft
- Maximum X position for the view's left edgemaxTop
- Maximum Y position for the view's top edge
-
continueSettling
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 againon the next frame to continue.- Parameters:
deferCallbacks
- true if state callbacks should be deferred via posted message.
-
isPointerDown
boolean isPointerDown(int pointerId)
Check if the given pointer ID represents a pointer that is currently down (to the bestof the ViewDragHelper's knowledge).
The state used to report this information is populated by the methods shouldInterceptTouchEvent or processTouchEvent. If one of these methods has notbeen called for all relevant MotionEvents to track, the information reportedby this method may be stale or incorrect.
- Parameters:
pointerId
- pointer ID to check; corresponds to IDs provided by MotionEvent
-
shouldInterceptTouchEvent
boolean shouldInterceptTouchEvent(@NonNull() MotionEvent ev)
Check if this event as provided to the parent view's onInterceptTouchEvent shouldcause the parent to intercept the touch event stream.
- Parameters:
ev
- MotionEvent provided to onInterceptTouchEvent
-
processTouchEvent
void processTouchEvent(@NonNull() MotionEvent ev)
Process a touch event received by the parent view. This method will dispatch callback eventsas needed before returning. The parent view's onTouchEvent implementation should call this.
- Parameters:
ev
- The touch event received by the parent view
-
checkTouchSlop
boolean checkTouchSlop(int directions)
Check if any pointer tracked in the current gesture has crossedthe required slop threshold.
This depends on internal state populated by shouldInterceptTouchEvent or processTouchEvent. You should only rely onthe results of this method after all currently available touch datahas been provided to one of these two methods.
- Parameters:
directions
- Combination of direction flags, see DIRECTION_HORIZONTAL,DIRECTION_VERTICAL, DIRECTION_ALL
-
checkTouchSlop
boolean checkTouchSlop(int directions, int pointerId)
Check if the specified pointer tracked in the current gesture has crossedthe required slop threshold.
This depends on internal state populated by shouldInterceptTouchEvent or processTouchEvent. You should only rely onthe results of this method after all currently available touch datahas been provided to one of these two methods.
- Parameters:
directions
- Combination of direction flags, see DIRECTION_HORIZONTAL,DIRECTION_VERTICAL, DIRECTION_ALLpointerId
- ID of the pointer to slop check as specified by MotionEvent
-
isEdgeTouched
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
- Edges to check for an initial edge touch.
-
isEdgeTouched
boolean isEdgeTouched(int edges, int pointerId)
Check if any of the edges specified were initially touched by the pointer withthe specified ID. If there is no currently active gesture or if there is no pointer withthe given ID currently down this method will return false.
- Parameters:
edges
- Edges to check for an initial edge touch.
-
isCapturedViewUnder
boolean isCapturedViewUnder(int x, int y)
Determine if the currently captured view is under the given point in theparent view's coordinate system. If there is no captured view this methodwill return false.
- Parameters:
x
- X position to test in the parent's coordinate systemy
- Y position to test in the parent's coordinate system
-
isViewUnder
boolean isViewUnder(@Nullable() View view, int x, int y)
Determine if the supplied view is under the given point in theparent view's coordinate system.
- Parameters:
view
- Child view of the parent to hit testx
- X position to test in the parent's coordinate systemy
- Y position to test in the parent's coordinate system
-
findTopChildUnder
@Nullable() 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 getOrderedChildIndex.
- Parameters:
x
- X position to test in the parent's coordinate systemy
- Y position to test in the parent's coordinate system
-
-
-
-