AutoScrollHelper
abstract class AutoScrollHelper : View.OnTouchListener
ListViewAutoScrollHelper |
An implementation of |
AutoScrollHelper is a utility class for adding automatic edge-triggered scrolling to Views.
Note: Implementing classes are responsible for overriding the scrollTargetBy, canTargetScrollHorizontally, and canTargetScrollVertically methods. See ListViewAutoScrollHelper for a android.widget.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:
- Delay after entering activation area before auto-scrolling begins, see
setActivationDelay. Default value isgetTapTimeoutto avoid conflicting with taps. - Location of activation areas, see
setEdgeType. Default value isEDGE_TYPE_INSIDE_EXTEND. - Size of activation areas relative to view size, see
setRelativeEdges. Default value is 20% for both vertical and horizontal edges. - Maximum size used to constrain relative size, see
setMaximumEdges. Default value isNO_MAX.
Scrolling
When automatic scrolling is active, the helper will repeatedly callscrollTargetBy to apply new scrolling offsets.
The following scrolling properties may be configured:
- Acceleration ramp-up duration, see
setRampUpDuration. Default value is 500 milliseconds. - Acceleration ramp-down duration, see
setRampDownDuration. Default value is 500 milliseconds. - Target velocity relative to view size, see
setRelativeVelocity. Default value is 100% per second for both vertical and horizontal. - Minimum velocity used to constrain relative velocity, see
setMinimumVelocity. 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. Default value is approximately 25 centimeters or 1575 dips per second.
Summary
Constants |
|
|---|---|
const Int |
EDGE_TYPE_INSIDE = 0Edge type that specifies an activation area starting at the view bounds and extending inward. |
const Int |
Edge type that specifies an activation area starting at the view bounds and extending inward. |
const Int |
Edge type that specifies an activation area starting at the view bounds and extending outward. |
const Float |
NO_MAX = 3.4028235E38fConstant passed to |
const Float |
NO_MIN = 0.0fConstant passed to |
const Float |
RELATIVE_UNSPECIFIED = 0.0fConstant passed to |
Public constructors |
|---|
AutoScrollHelper(target: View)Creates a new helper for scrolling the specified target view. |
Public functions |
|
|---|---|
abstract Boolean |
canTargetScrollHorizontally(direction: Int)Override this method to return whether the target view can be scrolled horizontally in a certain direction. |
abstract Boolean |
canTargetScrollVertically(direction: Int)Override this method to return whether the target view can be scrolled vertically in a certain direction. |
Boolean |
|
Boolean |
Indicates whether the scroll helper handles touch events exclusively during scrolling. |
Boolean |
onTouch(v: View!, event: MotionEvent!)Handles touch events by activating automatic scrolling, adjusting scroll velocity, or stopping. |
abstract Unit |
scrollTargetBy(deltaX: Int, deltaY: Int)Override this method to scroll the target view by the specified number of pixels. |
AutoScrollHelper |
setActivationDelay(delayMillis: Int)Sets the delay after entering an activation edge before activation of auto-scrolling. |
AutoScrollHelper |
setEdgeType(type: Int)Sets the activation edge type, one of: |
AutoScrollHelper! |
setEnabled(enabled: Boolean)Sets whether the scroll helper is enabled and should respond to touch events. |
AutoScrollHelper! |
setExclusive(exclusive: Boolean)Enables or disables exclusive handling of touch events during scrolling. |
AutoScrollHelper |
setMaximumEdges(horizontalMax: Float, verticalMax: Float)Sets the absolute maximum edge size. |
AutoScrollHelper |
setMaximumVelocity(horizontalMax: Float, verticalMax: Float)Sets the absolute maximum scrolling velocity. |
AutoScrollHelper |
setMinimumVelocity(horizontalMin: Float, verticalMin: Float)Sets the absolute minimum scrolling velocity. |
AutoScrollHelper |
setRampDownDuration(durationMillis: Int)Sets the amount of time after de-activation of auto-scrolling that is takes to slow to a stop. |
AutoScrollHelper |
setRampUpDuration(durationMillis: Int)Sets the amount of time after activation of auto-scrolling that is takes to reach target velocity for the current touch position. |
AutoScrollHelper |
setRelativeEdges(horizontal: Float, vertical: Float)Sets the activation edge size relative to the host view's dimensions. |
AutoScrollHelper |
setRelativeVelocity(horizontal: Float, vertical: Float)Sets the target scrolling velocity relative to the host view's dimensions. |
Constants
EDGE_TYPE_INSIDE
const val EDGE_TYPE_INSIDE = 0: Int
Edge type that specifies an activation area starting at the view bounds and extending inward. Moving outside the view bounds will stop scrolling.
| See also | |
|---|---|
setEdgeType |
EDGE_TYPE_INSIDE_EXTEND
const val EDGE_TYPE_INSIDE_EXTEND = 1: Int
Edge type that specifies an activation area starting at the view bounds and extending inward. After activation begins, moving outside the view bounds will continue scrolling.
| See also | |
|---|---|
setEdgeType |
EDGE_TYPE_OUTSIDE
const val EDGE_TYPE_OUTSIDE = 2: Int
Edge type that specifies an activation area starting at the view bounds and extending outward. Moving inside the view bounds will stop scrolling.
| See also | |
|---|---|
setEdgeType |
NO_MAX
const val NO_MAX = 3.4028235E38f: Float
Constant passed to setMaximumEdges, setMaximumVelocity, or setMinimumVelocity. Using this value ensures that the computed relative value is always used without constraining to a particular minimum or maximum value.
NO_MIN
const val NO_MIN = 0.0f: Float
Constant passed to setMaximumEdges, or setMaximumVelocity, or setMinimumVelocity. Using this value ensures that the computed relative value is always used without constraining to a particular minimum or maximum value.
RELATIVE_UNSPECIFIED
const val RELATIVE_UNSPECIFIED = 0.0f: Float
Constant passed to setRelativeEdges or setRelativeVelocity. Using this value ensures that the computed relative value is ignored and the absolute maximum value is always used.
Public constructors
AutoScrollHelper
AutoScrollHelper(target: View)
Creates a new helper for scrolling the specified target view.
The resulting helper may be configured by chaining setter calls and should be set as a touch listener on the target view.
By default, the helper is disabled and will not respond to touch events until it is enabled using setEnabled.
| Parameters | |
|---|---|
target: View |
The view to automatically scroll. |
Public functions
canTargetScrollHorizontally
abstract fun canTargetScrollHorizontally(direction: Int): Boolean
Override this method to return whether the target view can be scrolled horizontally in a certain direction.
| Parameters | |
|---|---|
direction: Int |
Negative to check scrolling left, positive to check scrolling right. |
| Returns | |
|---|---|
Boolean |
true if the target view is able to horizontally scroll in the specified direction. |
canTargetScrollVertically
abstract fun canTargetScrollVertically(direction: Int): Boolean
Override this method to return whether the target view can be scrolled vertically in a certain direction.
| Parameters | |
|---|---|
direction: Int |
Negative to check scrolling up, positive to check scrolling down. |
| Returns | |
|---|---|
Boolean |
true if the target view is able to vertically scroll in the specified direction. |
isEnabled
fun isEnabled(): Boolean
| Returns | |
|---|---|
Boolean |
True if this helper is enabled and responding to touch events. |
isExclusive
fun isExclusive(): Boolean
Indicates whether the scroll helper handles touch events exclusively during scrolling.
| Returns | |
|---|---|
Boolean |
True if exclusive handling of touch events during scrolling is enabled, false otherwise. |
| See also | |
|---|---|
setExclusive |
onTouch
fun onTouch(v: View!, event: MotionEvent!): Boolean
Handles touch events by activating automatic scrolling, adjusting scroll velocity, or stopping.
If isExclusive is false, always returns false so that the host view may handle touch events. Otherwise, returns true when automatic scrolling is active and false otherwise.
scrollTargetBy
abstract fun scrollTargetBy(deltaX: Int, deltaY: Int): Unit
Override this method to scroll the target view by the specified number of pixels.
setActivationDelay
fun setActivationDelay(delayMillis: Int): AutoScrollHelper
Sets the delay after entering an activation edge before activation of auto-scrolling. By default, the activation delay is set to getTapTimeout.
Specifying a delay of zero will start auto-scrolling immediately after the touch position enters an activation edge.
| Parameters | |
|---|---|
delayMillis: Int |
The activation delay in milliseconds. |
| Returns | |
|---|---|
AutoScrollHelper |
The scroll helper, which may used to chain setter calls. |
setEdgeType
fun setEdgeType(type: Int): AutoScrollHelper
Sets the activation edge type, one of:
EDGE_TYPE_INSIDEfor edges that respond to touches inside the bounds of the host view. If touch moves outside the bounds, scrolling will stop.EDGE_TYPE_INSIDE_EXTENDfor inside edges that continued to scroll when touch moves outside the bounds of the host view.EDGE_TYPE_OUTSIDEfor edges that only respond to touches that move outside the bounds of the host view.
| Parameters | |
|---|---|
type: Int |
The type of edge to use. |
| Returns | |
|---|---|
AutoScrollHelper |
The scroll helper, which may used to chain setter calls. |
setEnabled
fun setEnabled(enabled: Boolean): AutoScrollHelper!
Sets whether the scroll helper is enabled and should respond to touch events.
| Parameters | |
|---|---|
enabled: Boolean |
Whether the scroll helper is enabled. |
| Returns | |
|---|---|
AutoScrollHelper! |
The scroll helper, which may used to chain setter calls. |
setExclusive
fun setExclusive(exclusive: Boolean): AutoScrollHelper!
Enables or disables exclusive handling of touch events during scrolling. By default, exclusive handling is disabled and the target view receives all touch events.
When enabled, onTouch will return true if the helper is currently scrolling and false otherwise.
| Parameters | |
|---|---|
exclusive: Boolean |
True to exclusively handle touch events during scrolling, false to allow the target view to receive all touch events. |
| Returns | |
|---|---|
AutoScrollHelper! |
The scroll helper, which may used to chain setter calls. |
setMaximumEdges
fun setMaximumEdges(horizontalMax: Float, verticalMax: Float): AutoScrollHelper
Sets the absolute maximum edge size.
If relative edge size is not specified, activation edges will always be the maximum edge size. If both relative and maximum edges are specified, the maximum edge will be used to constrain the calculated relative edge size.
| Parameters | |
|---|---|
horizontalMax: Float |
The maximum horizontal edge size in pixels, or |
verticalMax: Float |
The maximum vertical edge size in pixels, or |
| Returns | |
|---|---|
AutoScrollHelper |
The scroll helper, which may used to chain setter calls. |
setMaximumVelocity
fun setMaximumVelocity(horizontalMax: Float, verticalMax: Float): AutoScrollHelper
Sets the absolute maximum scrolling velocity.
If relative velocity is not specified, scrolling will always reach the same maximum velocity. If both relative and maximum velocities are specified, the maximum velocity will be used to clamp the calculated relative velocity.
| Parameters | |
|---|---|
horizontalMax: Float |
The maximum horizontal scrolling velocity, or |
verticalMax: Float |
The maximum vertical scrolling velocity, or |
| Returns | |
|---|---|
AutoScrollHelper |
The scroll helper, which may used to chain setter calls. |
setMinimumVelocity
fun setMinimumVelocity(horizontalMin: Float, verticalMin: Float): AutoScrollHelper
Sets the absolute minimum scrolling velocity.
If both relative and minimum velocities are specified, the minimum velocity will be used to clamp the calculated relative velocity.
| Parameters | |
|---|---|
horizontalMin: Float |
The minimum horizontal scrolling velocity, or |
verticalMin: Float |
The minimum vertical scrolling velocity, or |
| Returns | |
|---|---|
AutoScrollHelper |
The scroll helper, which may used to chain setter calls. |
setRampDownDuration
fun setRampDownDuration(durationMillis: Int): AutoScrollHelper
Sets the amount of time after de-activation of auto-scrolling that is takes to slow to a stop.
Specifying a duration greater than zero prevents sudden jumps in velocity.
| Parameters | |
|---|---|
durationMillis: Int |
The ramp-down duration in milliseconds. |
| Returns | |
|---|---|
AutoScrollHelper |
The scroll helper, which may used to chain setter calls. |
setRampUpDuration
fun setRampUpDuration(durationMillis: Int): AutoScrollHelper
Sets the amount of time after activation of auto-scrolling that is takes to reach target velocity for the current touch position.
Specifying a duration greater than zero prevents sudden jumps in velocity.
| Parameters | |
|---|---|
durationMillis: Int |
The ramp-up duration in milliseconds. |
| Returns | |
|---|---|
AutoScrollHelper |
The scroll helper, which may used to chain setter calls. |
setRelativeEdges
fun setRelativeEdges(horizontal: Float, vertical: Float): AutoScrollHelper
Sets the activation edge size relative to the host view's dimensions.
If both relative and maximum edges are specified, the maximum edge will be used to constrain the calculated relative edge size.
| Parameters | |
|---|---|
horizontal: Float |
The horizontal edge size as a fraction of the host view width, or |
vertical: Float |
The vertical edge size as a fraction of the host view height, or |
| Returns | |
|---|---|
AutoScrollHelper |
The scroll helper, which may used to chain setter calls. |
setRelativeVelocity
fun setRelativeVelocity(horizontal: Float, vertical: Float): AutoScrollHelper
Sets the target scrolling velocity relative to the host view's dimensions.
If both relative and maximum velocities are specified, the maximum velocity will be used to clamp the calculated relative velocity.
| Parameters | |
|---|---|
horizontal: Float |
The target horizontal velocity as a fraction of the host view width per second, or |
vertical: Float |
The target vertical velocity as a fraction of the host view height per second, or |
| Returns | |
|---|---|
AutoScrollHelper |
The scroll helper, which may used to chain setter calls. |