Open sopharasum opened 3 years ago
kotlin code works like pager snap helper
val snapHelper : GravitySnapHelper = object : GravitySnapHelper(Gravity.TOP){ 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(binding?.yourRecyclerView)
Thank you brother for your snip of code. However, it does not work like a PagerSnapHelper when we swipe right or left.
Did you check this? https://github.com/rubensousa/GravitySnapHelper/blob/master/gravitysnaphelper/src/main/java/com/github/rubensousa/gravitysnaphelper/GravityPagerSnapHelper.java
How do you implement this function? As I am trying to attach it to RecyclerView
. Even though I am trying to replace GravitySnapHelper
with GravityPagerSnapHelper
on your custom object, still it does not work fine. If I have 3 items in HorizontalRecyclerView
, when I swipe right, it will jump to the last position.
Can you upload a sample showcasing the issue? GravityPagerSnapHelper only works if you have the items with "match_parent", same as ViewPager
like this? binding.snapHelper = GravitySnapHelper(Gravity.START).apply { maxFlingDistance = Your Screen Width }
@rubensousa Is it possible to work with items width are smaller than screen width?
kotlin code works like pager snap helper