viewpager setCurrentItem not changing tablayout selected color

andro picture andro · Feb 2, 2016 · Viewed 11.5k times · Source

i am using viewpager and tab layout

     viewPager.setAdapter(Adapter);
   tablayout.setupWithViewPager(viewPager);

   viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
        int cPage;

        @Override
        public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
        }

        @Override
        public void onPageSelected(int position) {


                cPage = position;

        }

        @Override
        public void onPageScrollStateChanged(int state) {

        }
    });

now when i swipe selected tab color is changing but when i call

                viewPager.setCurrentItem(1, true);

its not changing new tab layout color . say i have 2 tabs when i am 1st it setting text color green for 1st tab and when i swipe it giving 2nd tab color green .. but when i call

            viewPager.setCurrentItem(1, true);

in this my 1st tab is green not 2nd tab

Answer

BinqiangSun picture BinqiangSun · Mar 7, 2016

https://stackoverflow.com/a/35843370/4302046

// mViewPager.setCurrentItem(position);

mTabLayout.getTabAt(position).select();