viewPager.setCurrentItem(position, false) select, but doesn't scroll to highlighted Tab
Asked Answered
E

1

8

I have ViewPager with connected TabLayout. If I use viewPager.setCurrentItem(position, true) the correct tab on the tab layout is shown (TabLayout automatically scrolls to the selected tab). But I want to use viewPager.setCurrentItem(position, false).In this case correct tab is selected (highlighted) but the problem is that TabLayout doesn't scroll to this selected tab. It is hidden and I have to manual slide TabLayout to see what tab is selected. Is it a bug? Can I manually force TabLayout to scroll to the selected tab?

Update:

To be clear. My tab is properly selected and correct View is shown in ViewPager when performing viewPager.setCurrentItem(position, true) or viewPager.setCurrentItem(position, false). But with the second one, TabLayout doesn't scroll so my selected (highlighted) Tab is hidden.

Update 2:

I have opened the issue with a sample app: https://issuetracker.google.com/issues/72390853

Effeminize answered 23/1, 2018 at 17:28 Comment(5)
This Question is not clear. The "... correct tab is selected but the problem is that tab layout doesn't scroll to this selected tab"? Do you mean the Tab does not move or the ViewPager is not showing the correct View?Horgan
@Horgan Tab is not moved (selected tab is hidden), ViewPager shows correct View.Effeminize
Sorry my solution didn't work. I thought adding the TabLayout.Tab tab = mMainTabLayout.getTabAt(position); tab.select(); is what might be missing. Perhaps you could add all relevant code to your Post. Then we can step through your code to see exactly what is going on.Horgan
are you changing tab programmatically for a particular condition ?Khosrow
Before calling setCurrentItem I'm changing tabs in my PagerAdapter and call notifyDataSetChanged(). I have opened the issue with a sample app: issuetracker.google.com/issues/72390853Effeminize
W
0

Same issue, I use this workaround waiting a fix :

After I setup the viewPager2 and the tabLayout, I use a delay before setting the current item. I play with the invisibility of tabLayout to have a smoother display.

binding.tabLayout.isInvisible = true
binding.viewPager.apply {
    adapter = FragmentStateAdapter(this)
    TabLayoutMediator(binding.tabLayout, this) { tab, position ->
        tab.text = "text"
    }.attach()

    lifecycleScope.launch {
        delay(100)
        setCurrentItem(X, false)
        binding.tabLayout.isInvisible = false
    }
}
Washcloth answered 19/9, 2019 at 11:38 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.