romandanylyk / PageIndicatorView

An page indicator for Android ViewPager

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Example RecyclerView

flipnovidade opened this issue · comments

var thismidPos = 0
        var scrollX = 0
        recyclerView.addOnScrollListener(object:  RecyclerView.OnScrollListener() {
            override fun onScrolled(recyclerView: RecyclerView?, dx: Int, dy: Int) {
                super.onScrolled(recyclerView, dx, dy)

                scrollX += dx
                recyclerView?.getChildAt(0)?.width?.let {
                    val midPos = Math.floor(((scrollX + it / 2f) / it).toDouble()).toInt()
                    if (thismidPos != midPos) {
                        when {
                            thismidPos < midPos -> setCurrentItem((thismidPos+1), true)
                            else -> setCurrentItem((thismidPos-1), true)

                        }
                    }
                    thismidPos = midPos

                }
            }

            override fun onScrollStateChanged(recyclerView: RecyclerView?, newState: Int) {
                super.onScrollStateChanged(recyclerView, newState)
            }

        })

    private fun setCurrentItem(position: Int, smooth: Boolean) {
//        when(smooth){
//            true -> recyclerView.smoothScrollToPosition(position)
//            false -> recyclerView.scrollToPosition(position)
//        }
        pageIndicatorView.selection = position
    }

I hope to help others with this code

👍 ;)

library not upgraded for quite some time, it needs supports for viewpager2 and recyclerview