AutoScrollHelper

public abstract class AutoScrollHelper
extends Object implements View.OnTouchListener

java.lang.Object
   ↳ androidx.core.widget.AutoScrollHelper


AutoScrollHelper is a utility class for adding automatic edge-triggered scrolling to Views.

Note: Implementing classes are responsible for overriding the scrollTargetBy(int, int), canTargetScrollHorizontally(int), and canTargetScrollVertically(int) methods. See ListViewAutoScrollHelper for a ListView -specific implementation.

Activation

Automatic scrolling starts when the user touches within an activation area. By default, activation areas are defined as the top, left, right, and bottom 20% of the host view's total area. Touching within the top activation area scrolls up, left scrolls to the left, and so on.

As the user touches closer to the extreme edge of the activation area, scrolling accelerates up to a maximum velocity. When using the default edge type, EDGE_TYPE_INSIDE_EXTEND, moving outside of the view bounds will scroll at the maximum velocity.

The following activation properties may be configured:

Scrolling

When automatic scrolling is active, the helper will repeatedly call scrollTargetBy(int, int) to apply new scrolling offsets.

The following scrolling properties may be configured:

  • Acceleration ramp-up duration, see setRampUpDuration(int). Default value is 500 milliseconds.
  • Acceleration ramp-down duration, see setRampDownDuration(int). Default value is 500 milliseconds.
  • Target velocity relative to view size, see setRelativeVelocity(float, float). Default value is 100% per second for both vertical and horizontal.
  • Minimum velocity used to constrain relative velocity, see setMinimumVelocity(float, float). When set, scrolling will accelerate to the larger of either this value or the relative target value. Default value is approximately 5 centimeters or 315 dips per second.
  • Maximum velocity used to constrain relative velocity, see setMaximumVelocity(float, float). Default value is approximately 25 centimeters or 1575 dips per second.

Summary

Constants

int EDGE_TYPE_INSIDE

Edge type that specifies an activation area starting at the view bounds and extending inward.

int EDGE_TYPE_INSIDE_EXTEND

Edge type that specifies an activation area starting at the view bounds and extending inward.

int EDGE_TYPE_OUTSIDE

Edge type that specifies an activation area starting at the view bounds and extending outward.

float NO_MAX

Constant passed to setMaximumEdges(float, float), setMaximumVelocity(float, float), or setMinimumVelocity(float, float).

float NO_MIN

Constant passed to setMaximumEdges(float, float), or setMaximumVelocity(float, float), or setMinimumVelocity(float, float).

float RELATIVE_UNSPECIFIED

Constant passed to setRelativeEdges(float, float) or setRelativeVelocity(float, float).

Public constructors

AutoScrollHelper(View target)

Creates a new helper for scrolling the specified target view.

Public methods

abstract boolean canTargetScrollHorizontally(int direction)

Override this method to return whether the target view can be scrolled horizontally in a certain direction.

abstract boolean canTargetScrollVertically(int direction)

Override this method to return whether the target view can be scrolled vertically in a certain direction.

boolean isEnabled()
boolean isExclusive()

Indicates whether the scroll helper handles touch events exclusively during scrolling.

boolean onTouch(View v, MotionEvent event)

Handles touch events by activating automatic scrolling, adjusting scroll velocity, or stopping.

abstract void scrollTargetBy(int deltaX, int deltaY)

Override this method to scroll the target view by the specified number of pixels.

AutoScrollHelper setActivationDelay(int delayMillis)

Sets the delay after entering an activation edge before activation of auto-scrolling.

AutoScrollHelper setEdgeType(int type)

Sets the activation edge type, one of:

  • EDGE_TYPE_INSIDE for edges that respond to touches inside the bounds of the host view.

AutoScrollHelper setEnabled(boolean enabled)

Sets whether the scroll helper is enabled and should respond to touch events.

AutoScrollHelper setExclusive(boolean exclusive)

Enables or disables exclusive handling of touch events during scrolling.

AutoScrollHelper setMaximumEdges(float horizontalMax, float verticalMax)

Sets the absolute maximum edge size.

AutoScrollHelper setMaximumVelocity(float horizontalMax, float verticalMax)

Sets the absolute maximum scrolling velocity.