Azoft / CarouselLayoutManager

Android Carousel LayoutManager for RecyclerView
Apache License 2.0
2.56k stars 367 forks source link

Snap one by one #99

Open akifsabeh opened 5 years ago

akifsabeh commented 5 years ago

Is there an option to snap one item at a item no matter how fast the user flings?

root-ansh commented 5 years ago

i too request for this feature

Tantu194 commented 2 years ago

Hi guys @akifsabeh , @root-ansh I tried this, and it works fine

recyclerView.onFlingListener = object : RecyclerView.OnFlingListener() {
                override fun onFling(velocityX: Int, velocityY: Int): Boolean {
                    if (abs(velocityX) > 3000) {
                        var cur = layoutManager.centerItemPosition
                        if (velocityX > 0) {
                            cur += 1
                        } else {
                            cur -= 1
                        }
                        if (cur >= list.size) {
                            cur = 0
                        } else if (cur < 0) {
                            cur = list.size - 1
                        }
                        recyclerView.smoothScrollToPosition(cur)
                        return true
                    }
                    return false
                }
            }