NSouth NSouth - 1 year ago 148
Android Question

TabLayout tab text not highlighted after viewPager.setCurrentItem()

I'm having an issue with the TabLayout attached to my ViewPager. Repro steps:

  1. Start on the first tab.

  2. Select the 2nd tab.

  3. Press the back button--my code sees that the user is on the second tab and calls
    to return the user to the first tab.

  4. However, as shown in the picture, the 2nd tab text is still selected while the 1st tab text is grayed out. (Although the pink bar goes back to the 1st tab like it should.)

enter image description here

What am I missing?

tabLayout = (TabLayout) rootView.findViewById(R.id.tab_layout_main);

viewPager = (NonSwipeableViewPager) rootView.findViewById(R.id.pager_main);
pagerAdapter = new PagerAdapterMain(getActivity(), getChildFragmentManager(), tabLayout.getTabCount());

viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));

tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
public void onTabSelected(TabLayout.Tab tab) {

public void onTabUnselected(TabLayout.Tab tab) {

public void onTabReselected(TabLayout.Tab tab) {

Answer Source

you could try to select the tab through the tablayout instead of the viewPager.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download