1

I have TabLayout setup with a ViewPager. When I select a tab the ViewPager scroll to the selected page with animation (showing all the pages in between current page and selected page). I want ViewPager to directly jump to the selected page.

I know I can use viewPager.setCurrentItem(1, false) to disable smoothScroll. But I want to change the default behavior to disable smoothScroll effect on ViewPager

Here is what I have done.

val mSectionsPagerAdapter = SectionsPagerAdapter(supportFragmentManager)

        vp!!.adapter = mSectionsPagerAdapter
        vp!!.addOnPageChangeListener(object: ViewPager.OnPageChangeListener {
            override fun onPageScrollStateChanged(state: Int) {
            }

            override fun onPageScrolled(position: Int, positionOffset: Float, positionOffsetPixels: Int) {
            }

            override fun onPageSelected(position: Int) {
                when (position) {
                    0 -> supportActionBar!!.title = "Page 1"
                    1 -> supportActionBar!!.title = "Page 2"
                    2 -> supportActionBar!!.title = "Page 3"
                    3 -> supportActionBar!!.title = "Page 4"
                }
            }
        })

        tabLayout.setupWithViewPager(vp)
Xihuny
  • 1,410
  • 4
  • 19
  • 48

1 Answers1

0

You can subclass the ViewPager and make a custom ViewPager Please take a look at Toni's answer in this link: Disabling animation in ViewPager

Hamed Moghadasi
  • 1,523
  • 2
  • 16
  • 34
Dor
  • 657
  • 1
  • 5
  • 11