0

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 :

  1. Take the user to the corresponding fragment

  2. Set the TabLayout indicator accordingly

I managed to accomplish the first task using mViewPager.setCurrentItem() but I am unable to fix the second one. I tried :

  1. To retrieve the tab via the TabLayout and "select" it :

    mTabLayout.getTabAt(TAB_POSITION).select();
    

    but it was a bust. (Tab indicator)

  2. to call mTabLayout.setupWithViewPager(mViewPager); after each click on an item but 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 ?

Community
  • 1
  • 1
Mohammed Aouf Zouag
  • 17,042
  • 4
  • 41
  • 67

1 Answers1

0

In the method setupWithViewPager of class TabLayout, we can see that a callback is set on the view pager :

viewPager.addOnPageChangeListener(new TabLayoutOnPageChangeListener(this));

In the class TabLayoutOnPageChangeListener, here is the method onPageSelected :

public void onPageSelected(int position) {
final TabLayout tabLayout = mTabLayoutRef.get();
            if (tabLayout != null && tabLayout.getSelectedTabPosition() != position) {
                // Select the tab, only updating the indicator if we're not being dragged/settled
                // (since onPageScrolled will handle that).
                tabLayout.selectTab(tabLayout.getTabAt(position),
                        mScrollState == ViewPager.SCROLL_STATE_IDLE);
            }
        }

So here we can see that the tablayout should select the good tab when the viewpager changes its content.

Did you call setupWithViewPager right after setting the adapter of your ViewPager ?

Guillaume Imbert
  • 496
  • 5
  • 11
  • I do call setupWithViewPager after setting my ViewPager's adapter. Anyway, I tried to do what you said but it didn't work. – Mohammed Aouf Zouag Nov 04 '15 at 15:39
  • Did you try to debug to see if this method is really called? Can you show your code? – Guillaume Imbert Nov 04 '15 at 15:41
  • The question is not "did you debug" but "did you debug the method `onPageSelected` from `TabLayoutOnPageChangeListener`", which is not that obvious. `mTabLayout.getTabAt(0).select();` Using `setupWithPager` already does that for you, as shown in the `onPageSelected` code piece above. Maybe it has consequences setting two times in a row. Try removing those calls in your switch. – Guillaume Imbert Nov 04 '15 at 15:54