Skip to content

Instantly share code, notes, and snippets.

@jsaund
Created September 17, 2017 23:11
Show Gist options
  • Save jsaund/7cabe48f77f647673751d2c9f2a072d2 to your computer and use it in GitHub Desktop.
Save jsaund/7cabe48f77f647673751d2c9f2a072d2 to your computer and use it in GitHub Desktop.
SnapHelper tip: Limit the time a scroll takes
private const val MILLISECONDS_PER_INCH = 100
private const val MAX_SCROLL_ON_FLING_DURATION = 250 // ms
override fun createSnapScroller(layoutManager: LayoutManager?): LinearSmoothScroller? {
if (layoutManager !is ScrollVectorProvider) return null
return object : LinearSmoothScroller(context) {
override fun onTargetFound(targetView: View?, state: State?, action: Action?) {
if (targetView == null) return
val snapDistances = calculateDistanceToFinalSnap(layoutManager, targetView)
val dx = snapDistances[0]
val dy = snapDistances[1]
val time = calculateTimeForDeceleration(Math.max(Math.abs(dx), Math.abs(dy)))
if (time > 0) {
action?.update(dx, dy, time, mDecelerateInterpolator)
}
}
override fun calculateSpeedPerPixel(displayMetrics: DisplayMetrics?): Float =
MILLISECONDS_PER_INCH / displayMetrics.densityDpi
override fun calculateTimeForScrolling(dx: Int): Int =
Math.min(MAX_SCROLL_ON_FLING_DURATION, super.calculateTimeForScrolling(dx))
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment