Devlight / InfiniteCycleViewPager

Infinite cycle ViewPager with two-way orientation and interactive effect.
Apache License 2.0
5.75k stars 831 forks source link

PageIndicator #69

Open rajeesh7 opened 6 years ago

rajeesh7 commented 6 years ago

How can we add page indicator, the dot image to bottom of this viewpager ? On page scroll these dots must indicate the position of the current item

MohNage7 commented 5 years ago

Did you find any solution for it ?

cj3dreams commented 2 years ago

Feeling how I being more smarter year by year :D I used that in fragment + with PageIndicatorView (https://github.com/romandanylyk/PageIndicatorView) it works fine! These are my codes :) enjoy

class HomeFragment : Fragment() {
    private lateinit var horizontalInfiniteCycleViewPager: HorizontalInfiniteCycleViewPager
    private lateinit var dotsIndicator: PageIndicatorView

    private var previousPage = 0
    private var currentPage = -1

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?): View? {

        val view =  inflater.inflate(R.layout.fragment_home, container, false)

        horizontalInfiniteCycleViewPager = view.findViewById(R.id.homeHorizonInfCycleViewPager)
        dotsIndicator = view.findViewById(R.id.homeDotsIndicator)

        return view
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)

        horizontalInfiniteCycleViewPager.adapter =
            AccountsAdapter((0..2).toList(), requireContext())
        dotsIndicator.count = 3 //Static count of pages, just for testing, I used for operator "if" only 3 pages!!! manually, but u can rewrite it with dynamic count
        dotsIndicator.setAnimationType(AnimationType.WORM)
        horizontalInfiniteCycleViewPager.addOnPageChangeListener(object: ViewPager.OnPageChangeListener{
            override fun onPageScrolled(position: Int, positionOffset: Float, positionOffsetPixels: Int) {
                if (previousPage<position) {
                    if (currentPage !=2 ) currentPage++ else currentPage = 0
                    dotsIndicator.selection = currentPage
                    previousPage = position
                }
                else if (previousPage>position) {
                    if (currentPage == 0) currentPage = 2 else currentPage--
                    dotsIndicator.selection = currentPage
                    previousPage = position
                }
            }override fun onPageSelected(position: Int) {}
            override fun onPageScrollStateChanged(state: Int) {} })
   }
}