public class SwipeDismissBehavior<V extends View> extends CoordinatorLayout.Behavior<V>
CoordinatorLayout
to provide support
for the 'swipe-to-dismiss' gesture.Modifier and Type | Class and Description |
---|---|
static interface |
SwipeDismissBehavior.OnDismissListener
Callback interface used to notify the application that the view has been dismissed.
|
Modifier and Type | Field and Description |
---|---|
static int |
STATE_DRAGGING
A view is currently being dragged.
|
static int |
STATE_IDLE
A view is not currently being dragged or animating as a result of a fling/snap.
|
static int |
STATE_SETTLING
A view is currently settling into place as a result of a fling or
predefined non-interactive motion.
|
static int |
SWIPE_DIRECTION_ANY
Swipe direction which allows swiping in either direction.
|
static int |
SWIPE_DIRECTION_END_TO_START
Swipe direction that only allows swiping in the direction of end-to-start.
|
static int |
SWIPE_DIRECTION_START_TO_END
Swipe direction that only allows swiping in the direction of start-to-end.
|
Constructor and Description |
---|
SwipeDismissBehavior() |
Modifier and Type | Method and Description |
---|---|
boolean |
canSwipeDismissView(View view)
Called when the user's input indicates that they want to swipe the given view.
|
int |
getDragState()
Retrieve the current drag state of this behavior.
|
boolean |
onInterceptTouchEvent(CoordinatorLayout parent,
V child,
MotionEvent event)
Respond to CoordinatorLayout touch events before they are dispatched to child views.
|
boolean |
onTouchEvent(CoordinatorLayout parent,
V child,
MotionEvent event)
Respond to CoordinatorLayout touch events after this Behavior has started
intercepting them. |
void |
setDragDismissDistance(float distance)
Set the threshold for telling if a view has been dragged enough to be dismissed.
|
void |
setEndAlphaSwipeDistance(float fraction)
The maximum swipe distance for the view's alpha is modified.
|
void |
setListener(SwipeDismissBehavior.OnDismissListener listener)
Set the listener to be used when a dismiss event occurs.
|
void |
setSensitivity(float sensitivity)
Set the sensitivity used for detecting the start of a swipe.
|
void |
setStartAlphaSwipeDistance(float fraction)
The minimum swipe distance before the view's alpha is modified.
|
void |
setSwipeDirection(int direction)
Sets the swipe direction for this behavior.
|
blocksInteractionBelow, getInsetDodgeRect, getScrimColor, getScrimOpacity, getTag, isDirty, layoutDependsOn, onApplyWindowInsets, onAttachedToLayoutParams, onDependentViewChanged, onDependentViewRemoved, onDetachedFromLayoutParams, onLayoutChild, onMeasureChild, onNestedFling, onNestedPreFling, onNestedPreScroll, onNestedScroll, onNestedScrollAccepted, onRequestChildRectangleOnScreen, onRestoreInstanceState, onSaveInstanceState, onStartNestedScroll, onStopNestedScroll, setTag
public static final int STATE_IDLE
public static final int STATE_DRAGGING
public static final int STATE_SETTLING
public static final int SWIPE_DIRECTION_START_TO_END
public static final int SWIPE_DIRECTION_END_TO_START
public static final int SWIPE_DIRECTION_ANY
public void setListener(SwipeDismissBehavior.OnDismissListener listener)
listener
- the listener to use.public void setSwipeDirection(int direction)
direction
- one of the SWIPE_DIRECTION_START_TO_END
,
SWIPE_DIRECTION_END_TO_START
or SWIPE_DIRECTION_ANY
public void setDragDismissDistance(float distance)
distance
- a ratio of a view's width, values are clamped to 0 >= x <= 1f;public void setStartAlphaSwipeDistance(float fraction)
fraction
- the distance as a fraction of the view's width.public void setEndAlphaSwipeDistance(float fraction)
fraction
- the distance as a fraction of the view's width.public void setSensitivity(float sensitivity)
sensitivity
- Multiplier for how sensitive we should be about detecting
the start of a drag. Larger values are more sensitive. 1.0f is normal.public boolean onInterceptTouchEvent(CoordinatorLayout parent, V child, MotionEvent event)
CoordinatorLayout.Behavior
Behaviors can use this to monitor inbound touch events until one decides to intercept the rest of the event stream to take an action on its associated child view. This method will return false until it detects the proper intercept conditions, then return true once those conditions have occurred.
Once a Behavior intercepts touch events, the rest of the event stream will
be sent to the CoordinatorLayout.Behavior.onTouchEvent(android.support.design.widget.CoordinatorLayout, V, android.view.MotionEvent)
method.
The default implementation of this method always returns false.
onInterceptTouchEvent
in class CoordinatorLayout.Behavior<V extends View>
parent
- the parent view currently receiving this touch eventchild
- the child view associated with this Behaviorevent
- the MotionEvent describing the touch event being processedpublic boolean onTouchEvent(CoordinatorLayout parent, V child, MotionEvent event)
CoordinatorLayout.Behavior
intercepting
them.
Behaviors may intercept touch events in order to help the CoordinatorLayout manipulate its child views. For example, a Behavior may allow a user to drag a UI pane open or closed. This method should perform actual mutations of view layout state.
onTouchEvent
in class CoordinatorLayout.Behavior<V extends View>
parent
- the parent view currently receiving this touch eventchild
- the child view associated with this Behaviorevent
- the MotionEvent describing the touch event being processedpublic boolean canSwipeDismissView(@NonNull View view)
view
- View the user is attempting to swipepublic int getDragState()
STATE_IDLE
, STATE_DRAGGING
or STATE_SETTLING
.