I'm having an issue with the TabLayout attached to my ViewPager. Repro steps:
viewPager.setCurrentItem(0)
to return the user to the first tab. What am I missing?
tabLayout = (TabLayout) rootView.findViewById(R.id.tab_layout_main);
tabLayout.addTab(tabLayout.newTab().setText(getActivity().getString(R.string.main_tab_grades)));
tabLayout.addTab(tabLayout.newTab().setText(getActivity().getString(R.string.main_tab_schedule)));
viewPager = (NonSwipeableViewPager) rootView.findViewById(R.id.pager_main);
pagerAdapter = new PagerAdapterMain(getActivity(), getChildFragmentManager(), tabLayout.getTabCount());
viewPager.setAdapter(pagerAdapter);
viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
@Override
public void onTabSelected(TabLayout.Tab tab) {
viewPager.setCurrentItem(tab.getPosition());
}
@Override
public void onTabUnselected(TabLayout.Tab tab) {
return;
}
@Override
public void onTabReselected(TabLayout.Tab tab) {
return;
}
});
Maybe it is a bug of Design Library. As the issue said: https://code.google.com/p/android/issues/detail?id=192834
And the codes worked for me :
// mViewPager.setCurrentItem(position);
mTabLayout.getTabAt(position).select();
you could try to select the tab through the tablayout instead of the viewPager.
tabLayout.getTabAt(0).select();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With