TabLayout tab text is not highlighted after viewPager.setCurrentItem ()

I am having a problem with a TabLayout attached to my ViewPager. Repro Actions:

  • Start with the first tab.
  • Select the second tab.
  • Click the "Back" button - my code sees that the user is on the second tab and calls viewPager.setCurrentItem(0) to return the user to the first tab.
  • However, as shown in the figure, the text of the second tab is still selected, and the text of the first tab is grayed out. (Although the pink bar returns to the 1st tab, as it should.)

enter image description here

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; } }); 
+3
source share
7 answers

you can try to select a tab via tablayout instead of viewPager.

 tabLayout.getTabAt(0).select(); 
+5
source

Perhaps this is a design library error. As the question says: https://code.google.com/p/android/issues/detail?id=192834

And the codes worked for me:

//mViewPager.setCurrentItem(position);

 mTabLayout.getTabAt(position).select(); 
+5
source

Faced with a unique challenge. When we set setCurrentItem. It does not change the tablayout tab. Then you need to add AddOnPageChangeListener to the viewpager, in which you need to select the tablayout tab manually for the selected viewpager position. Then setupWithViewPager.

Note. setupWithViewPager needs to be set only after adding addOnPageChangeListener. God knows why. This is what worked. if i setupWithViewPager before, it doesn't work. Again, the almighty knows.

 viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() { @Override public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) { } @Override public void onPageSelected(int position) { viewPager.setCurrentItem(position,false); tabLayout.getTabAt(position).select(); } @Override public void onPageScrollStateChanged(int state) { } }); /* NOTE: This is setup after addOnPageChangeListener. Don't know why but this is what works. Otherwise tabLayout.does not select. */ tabLayout.setupWithViewPager(this.viewPager); 
+1
source

none of the above actions ... finally, the scroll position is not fixed

 tabLayout.setScrollPosition(position,0f,true); 
+1
source

tabLayout.getTabAt (0) .getCustomView () setSelected (true). I do not think this is a mistake for TabLayout, if you want to set the first kind of highlight, you should call a method similar to me, then the customView you set will be invalid.

0
source

I will add that this can fix this error: tabLayout.getTabAt (0) .getCustomView () setSelected (true) ;.

0
source

I fixed its update to com.android.support:design:23.4.0 version 23.1.0 version.

0
source

All Articles