My question has been answered several times when it has TabActivity
with tabWidgets
. But I could not find anything regarding the relationally new TabLayout
.
It's quite simple, just like on facebook, I want to click on a tab that is already selected so that my list scrolls up to its beginning. but I can’t figure out where to put the listener.
I tried TabLayout
, mTabLayout.getChildAt()
and TabLayout.getTabAt().getCustomView()
.
EDIT: Bugfix: As CommonsWare comments in a comment on his answer, I had to rewrite the "onTabSelected" behavior.
mTabLayout = (TabLayout) findViewById(R.id.tabs); mTabLayout.setupWithViewPager(mViewPager); for (int i = 0; i < mTabLayout.getTabCount(); i++) { mTabLayout.getTabAt(i).setIcon(tabIcons[i]); } mTabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() { @Override public void onTabSelected(TabLayout.Tab tab) { mViewPager.setCurrentItem(tab.getPosition()); } @Override public void onTabUnselected(TabLayout.Tab tab) { } @Override public void onTabReselected(TabLayout.Tab tab) { if (tab.getPosition() == PAGE_FEED) { ((PagerNewsFeedFragment) mViewPagerAdapter.getRegisteredFragment(PAGE_FEED)).scrollToTop(); } } });
Thanks!
source share