Nilzor
Nilzor

Reputation: 18593

How can I listen to onTabReselected in a TabLayout bound to a ViewPager?

So I'm using TabLayout.setupWithViewPager to make my tabs swipable. In addition I want special functionality attached to a tabReselcted event on the tabs - normally implemented by adding a onTabSelectedListener. The TabLayout can only have one at a time though - the ViewPager now owns that event pipe. How can I solve this conflict?

Anyone have a good solution where I still can use the convenience method setupWithViewPager? If not, solve it by a minimal amount of line of codes?

Upvotes: 0

Views: 188

Answers (2)

jaibatrik
jaibatrik

Reputation: 7533

You can try using TabLayout.ViewPagerOnTabSelectedListener. It contains necessary calls back to the provided ViewPager so that the tab position is kept in sync. It has onTabReselected method you are looking for and is designed to work with ViewPager. Find the documentation here.

Upvotes: 1

Victor Gomes
Victor Gomes

Reputation: 128

that should not be happening if you add the onTabSelectedListener after the TabLayout.setupWithViewPager.

I just tested it. Are you doing it in this order?

Edit: Do the following if the fragments don't change:

public void onTabSelected(TabLayout.Tab tab) { pager.setCurrentItem(tab.getPosition()); }

Upvotes: 1

Related Questions