Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Disable animation in viewpager2

I have viewpager2 and adapter for him that extends FragmentStateAdapter. I want user to go to another page only by clicking on tablayout. I have disabled user input for this viewpager2. But when I click on tab, there is animation of fast swiping between pages. But I want just new fragment to show. Like with FragmentTransaction, but with viewpager2 and tablayout. Does anyone knows ho to do it?

like image 392
DenBondd Avatar asked Apr 19 '20 09:04

DenBondd


2 Answers

You should use addOnTabSelectedListener like this:

    tabLayout.addOnTabSelectedListener(object : TabLayout.OnTabSelectedListener {
        override fun onTabReselected(tab: TabLayout.Tab?) {
        }

        override fun onTabUnselected(tab: TabLayout.Tab?) {
        }

        override fun onTabSelected(tab: TabLayout.Tab?) {
            tab?.position?.let { viewPager?.setCurrentItem(it, false) }
        }

    })

You already use

viewPager.isUserInputEnabled = false

Note : setCurrentItem(int item, boolean smoothScroll)

Set the currently selected page with smooth scroll. If you set smooth scroll is false, you don't see the animation

like image 131
Kasım Özdemir Avatar answered Sep 30 '22 16:09

Kasım Özdemir


TabLayoutMediator is what you need. Just set smoothScroll to false.

public TabLayoutMediator(
  @NonNull TabLayout tabLayout,
  @NonNull ViewPager2 viewPager,
  boolean autoRefresh,
  boolean smoothScroll,
  @NonNull TabConfigurationStrategy tabConfigurationStrategy) 

https://developer.android.com/reference/com/google/android/material/tabs/TabLayoutMediator

like image 36
Ocor Kcirad Avatar answered Sep 30 '22 16:09

Ocor Kcirad