I have a NavigationView that I want to setup to work along with a ViewPager and a TabLayout.
A click on an item from the NavigationView shall :
Take the user to the corresponding
fragmentSet the
TabLayoutindicator accordingly
I managed to accomplish the first task using mViewPager.setCurrentItem() but I am unable to fix the second one. I tried :
To retrieve the
tabvia theTabLayoutand "select" it :mTabLayout.getTabAt(TAB_POSITION).select();but it was a bust. (Tab indicator)
to call
mTabLayout.setupWithViewPager(mViewPager);after each click on anitembut same result. (TabLayout tab selection).
EDIT - code :
mViewPager = (ViewPager) findViewById(R.id.viewpager);
mViewPager.setAdapter(mAdapter);
mTabLayout = (TabLayout) findViewById(R.id.tablayout);
mTabLayout.setupWithViewPager(mViewPager);
mViewPager.addOnPageChangeListener(new PageChangeListener());
..... .....
class PageChangeListener implements ViewPager.OnPageChangeListener {
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
@Override
public void onPageSelected(int position) {
switch (position) {
case 0:
mTabLayout.getTabAt(0).select();
break;
case 1:
mFloatingAddButton.setVisibility(View.VISIBLE);
mTabLayout.getTabAt(1).select();
break;
default:
mFloatingAddButton.setVisibility(View.INVISIBLE);
mTabLayout.getTabAt(2).select();
break;
}
}
@Override
public void onPageScrollStateChanged(int state) {
}
}
How can I manage to tie everything together ?