Search code examples
javaandroidandroid-fragmentsandroid-pageradapterandroid-collapsingtoolbarlayout

Undoing Collapse Toolbar When Clicking on Another Tab


I have an app with three tabs and collapsible app bar. One of the tab fragments is a map so it doesn't close the app bar when I move it up and down, and two other fragments are recyclerview lists. When I scroll them my app bar disappears and appears.

Now I want to enable another thing which is this: When the user has collapsed the app bar on one tab, and he clicks another tab, the app bar should reappear.

I searched, but without success.

I enable collapsing my app bar like this:

<android.support.design.widget.AppBarLayout
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="?attr/colorPrimary"
        app:layout_scrollFlags="scroll|enterAlways"
        app:popupTheme="@style/AppTheme.PopupOverlay"
        app:tabSelectedTextColor="@color/ppdBurntOrange" />

</android.support.design.widget.AppBarLayout>

My pager adapter:

public class PagerAdapter extends FragmentStatePagerAdapter {
private int mNumOfTabs;

public PagerAdapter(FragmentManager fm, int NumOfTabs) {
    super(fm);
    this.mNumOfTabs = NumOfTabs;
}

@Override
public Fragment getItem(int position) {

    switch (position) {
        case 0:
            return new MapsFragment();
        case 1:
            return new RestauransFragment();
        case 2:
            return new MyRestauransFragment();
        default:
            return null;
    }
}

@Override
public int getCount() {
    return mNumOfTabs;
}

My main activity:

TabLayout tabLayout = findViewById(R.id.sliding_tabs);
tabLayout.addTab(tabLayout.newTab().setText(getString(R.string.tab_one)));
tabLayout.addTab(tabLayout.newTab().setText(getString(R.string.tab_two)));
tabLayout.addTab(tabLayout.newTab().setText(getString(R.string.tab_three)));
tabLayout.setTabGravity(TabLayout.GRAVITY_FILL);
tabLayout.setTabMode(TabLayout.MODE_FIXED);

final ViewPager viewPager = findViewById(R.id.viewpager);
PagerAdapter adapter = new PagerAdapter
            (getSupportFragmentManager(), tabLayout.getTabCount());
viewPager.setAdapter(adapter);
viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
viewPager.setOffscreenPageLimit(3);
tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
    @Override
    public void onTabSelected(TabLayout.Tab tab) {
        viewPager.setCurrentItem(tab.getPosition());
    }

    @Override
    public void onTabUnselected(TabLayout.Tab tab) {
    }

    @Override
    public void onTabReselected(TabLayout.Tab tab) {
    }
});

Solution

  • Found a solution here.

    I added:

    AppBarLayout appBarLayout;
    appBarLayout = findViewById(R.id.appBarMainActivity);
    ...
    public void onTabSelected(TabLayout.Tab tab) {
        viewPager.setCurrentItem(tab.getPosition());
        appBarLayout.setExpanded(true);
    }
    

    Hope this will help someone.