added in version 22.2.0
belongs to Maven artifact com.android.support:design:27.1.0

SwipeDismissBehavior

public class SwipeDismissBehavior
extends Behavior<V extends View>

java.lang.Object
   ↳ android.support.design.widget.CoordinatorLayout.Behavior<V extends android.view.View>
     ↳ android.support.design.widget.SwipeDismissBehavior<V extends android.view.View>


An interaction behavior plugin for child views of CoordinatorLayout to provide support for the 'swipe-to-dismiss' gesture.

Summary

Nested classes

interface SwipeDismissBehavior.OnDismissListener

Callback interface used to notify the application that the view has been dismissed. 

Constants

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.

int SWIPE_DIRECTION_ANY

Swipe direction which allows swiping in either direction.

int SWIPE_DIRECTION_END_TO_START

Swipe direction that only allows swiping in the direction of end-to-start.

int SWIPE_DIRECTION_START_TO_END

Swipe direction that only allows swiping in the direction of start-to-end.

Public constructors

SwipeDismissBehavior()

Public methods

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.

Inherited methods

From class android.support.design.widget.CoordinatorLayout.Behavior
From class java.lang.Object

Constants

STATE_DRAGGING

added in version 22.2.0
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

added in version 22.2.0
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

added in version 22.2.0
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)

SWIPE_DIRECTION_ANY

added in version 22.2.0
int SWIPE_DIRECTION_ANY

Swipe direction which allows swiping in either direction.

Constant Value: 2 (0x00000002)

SWIPE_DIRECTION_END_TO_START

added in version 22.2.0
int SWIPE_DIRECTION_END_TO_START

Swipe direction that only allows swiping in the direction of end-to-start. That is right-to-left in LTR or left-to-right in RTL.

Constant Value: 1 (0x00000001)

SWIPE_DIRECTION_START_TO_END

added in version 22.2.0
int SWIPE_DIRECTION_START_TO_END

Swipe direction that only allows swiping in the direction of start-to-end. That is left-to-right in LTR, or right-to-left in RTL.

Constant Value: 0 (0x00000000)

Public constructors

SwipeDismissBehavior

added in version 22.2.0
SwipeDismissBehavior ()

Public methods

canSwipeDismissView

added in version 23.4.0
boolean canSwipeDismissView (View view)

Called when the user's input indicates that they want to swipe the given view.

Parameters
view View: View the user is attempting to swipe

Returns
boolean true if the view can be dismissed via swiping, false otherwise

getDragState

added in version 22.2.0
int getDragState ()

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

Returns
int The current drag state

onInterceptTouchEvent

added in version 22.2.0
boolean onInterceptTouchEvent (CoordinatorLayout parent, 
                V child, 
                MotionEvent event)

Respond to CoordinatorLayout touch events before they are dispatched to child views.

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 onTouchEvent(CoordinatorLayout, V, MotionEvent) method.

This method will be called regardless of the visibility of the associated child of the behavior. If you only wish to handle touch events when the child is visible, you should add a check to isShown() on the given child.

The default implementation of this method always returns false.

Parameters
parent CoordinatorLayout: the parent view currently receiving this touch event

child V: the child view associated with this Behavior

event MotionEvent: the MotionEvent describing the touch event being processed

Returns
boolean true if this Behavior would like to intercept and take over the event stream. The default always returns false.

onTouchEvent

added in version 22.2.0
boolean onTouchEvent (CoordinatorLayout parent, 
                V child, 
                MotionEvent event)

Respond to CoordinatorLayout touch events after this Behavior has started 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.

This method will be called regardless of the visibility of the associated child of the behavior. If you only wish to handle touch events when the child is visible, you should add a check to isShown() on the given child.

Parameters
parent CoordinatorLayout: the parent view currently receiving this touch event

child V: the child view associated with this Behavior

event MotionEvent: the MotionEvent describing the touch event being processed

Returns
boolean true if this Behavior handled this touch event and would like to continue receiving events in this stream. The default always returns false.

setDragDismissDistance

added in version 22.2.0
void setDragDismissDistance (float distance)

Set the threshold for telling if a view has been dragged enough to be dismissed.

Parameters
distance float: a ratio of a view's width, values are clamped to 0 >= x <= 1f;

setEndAlphaSwipeDistance

added in version 22.2.0
void setEndAlphaSwipeDistance (float fraction)

The maximum swipe distance for the view's alpha is modified.

Parameters
fraction float: the distance as a fraction of the view's width.

setListener

added in version 22.2.0
void setListener (SwipeDismissBehavior.OnDismissListener listener)

Set the listener to be used when a dismiss event occurs.

Parameters
listener SwipeDismissBehavior.OnDismissListener: the listener to use.

setSensitivity

added in version 22.2.0
void setSensitivity (float sensitivity)

Set the sensitivity used for detecting the start of a swipe. This only takes effect if no touch handling has occured yet.

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

setStartAlphaSwipeDistance

added in version 22.2.0
void setStartAlphaSwipeDistance (float fraction)

The minimum swipe distance before the view's alpha is modified.

Parameters
fraction float: the distance as a fraction of the view's width.

setSwipeDirection

added in version 22.2.0
void setSwipeDirection (int direction)

Sets the swipe direction for this behavior.

Parameters
direction int: one of the SWIPE_DIRECTION_START_TO_END, SWIPE_DIRECTION_END_TO_START or SWIPE_DIRECTION_ANY