Skip to content

Instantly share code, notes, and snippets.

@jsaund
Created September 17, 2017 23:07
Show Gist options
  • Save jsaund/6c724114cbc45efaa4281e68ff8436ae to your computer and use it in GitHub Desktop.
Save jsaund/6c724114cbc45efaa4281e68ff8436ae to your computer and use it in GitHub Desktop.
SnapHelper tip: limit the maximum fling distance
private var scroller: Scroller? = null
private val minX = -2000
private val maxX = 2000
private val minY = -2000
private val maxY = 2000
override fun attachToRecyclerView(recyclerView: RecyclerView?) {
scroller = Scroller(recyclerView?.context, DecelerateInterpolator())
super.attachToRecyclerView(recyclerView)
}
override fun calculateScrollDistance(velocityX: Int, velocityY: Int): IntArray {
val out = IntArray(2)
scroller?.fling(0, 0, velocityX, velocityY, minX, maxX, minY, maxY)
out[0] = scroller?.finalX ?: 0
out[1] = scroller?.finalY ?: 0
return out
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment