rubensousa / GravitySnapHelper

A SnapHelper that snaps a RecyclerView to an edge.
Apache License 2.0
5k stars 615 forks source link

Does this can work like PagerSnapHelper #70

Open sopharasum opened 3 years ago

RohanArora13 commented 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)
sopharasum commented 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.

rubensousa commented 3 years ago

Did you check this? https://github.com/rubensousa/GravitySnapHelper/blob/master/gravitysnaphelper/src/main/java/com/github/rubensousa/gravitysnaphelper/GravityPagerSnapHelper.java

sopharasum commented 3 years ago

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.

rubensousa commented 3 years ago

Can you upload a sample showcasing the issue? GravityPagerSnapHelper only works if you have the items with "match_parent", same as ViewPager

giagor commented 2 years ago

like this? binding.snapHelper = GravitySnapHelper(Gravity.START).apply { maxFlingDistance = Your Screen Width }

charlie-yang-gogox commented 9 months ago

@rubensousa Is it possible to work with items width are smaller than screen width?