Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Does this can work like PagerSnapHelper #70

Open
sopharasum opened this issue Dec 7, 2020 · 7 comments
Open

Does this can work like PagerSnapHelper #70

sopharasum opened this issue Dec 7, 2020 · 7 comments

Comments

@sopharasum
Copy link

No description provided.

@RohanArora13
Copy link

RohanArora13 commented Jan 26, 2021

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
Copy link
Author

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.

@sopharasum
Copy link
Author

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
Copy link
Owner

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

@giagor
Copy link

giagor commented Nov 19, 2021

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

@charlie-yang-gogox
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants