Skip to content

Instantly share code, notes, and snippets.

@CodeK1988
Created April 26, 2019 09:45
Show Gist options
  • Save CodeK1988/ce65294124b279139f243096a134daca to your computer and use it in GitHub Desktop.
Save CodeK1988/ce65294124b279139f243096a134daca to your computer and use it in GitHub Desktop.
var snapHelper: LinearSnapHelper = object : LinearSnapHelper() {
override fun findTargetSnapPosition(layoutManager: RecyclerView.LayoutManager?, velocityX: Int, velocityY: Int): Int {
val centerView = findSnapView(layoutManager!!) ?: return RecyclerView.NO_POSITION
val position = layoutManager.getPosition(centerView)
var targetPosition = -1
if (layoutManager.canScrollHorizontally()) {
targetPosition = if (velocityX < 0) {
position - 1
} else {
position + 1
}
}
if (layoutManager.canScrollVertically()) {
targetPosition = if (velocityY < 0) {
position - 1
} else {
position + 1
}
}
val firstItem = 0
val lastItem = layoutManager.itemCount - 1
targetPosition = Math.min(lastItem, Math.max(targetPosition, firstItem))
return targetPosition
}
}
snapHelper.attachToRecyclerView(recyclerView)
@CodeK1988
Copy link
Author

LinearSnapHelper 居中问题

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment