简体   繁体   中英

Collapse CollapsingToolbarLayout on swiping between tabs

I'm using a CollapsingToolbarLayout and a viewpage for my fragment (tabs).

<android.support.design.widget.CoordinatorLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:app="http://schemas.android.com/apk/res-auto"
  xmlns:tools="http://schemas.android.com/tools"
  android:layout_width="match_parent"
  android:layout_height="match_parent">

  <android.support.design.widget.AppBarLayout
      android:id="@+id/appbar"
      android:layout_width="match_parent"
      android:layout_height="150dp"
      android:fitsSystemWindows="true"
      android:theme="@style/AppTheme.AppBarOverlay">

      <android.support.design.widget.CollapsingToolbarLayout
          android:id="@+id/collapsing_toolbar"
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          android:fitsSystemWindows="true"
          app:contentScrim="?attr/colorPrimary"
          app:layout_scrollFlags="scroll|exitUntilCollapsed"
          >

          <fragment                         
              xmlns:android="http://schemas.android.com/apk/res/android"
              android:id="@+id/map"
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              class="com.google.android.gms.maps.SupportMapFragment"/>

          <android.support.v7.widget.Toolbar
              android:id="@+id/toolbar"
              android:layout_width="match_parent"
              android:layout_height="?attr/actionBarSize"
              app:popupTheme="@style/AppTheme.PopupOverlay">

          </android.support.v7.widget.Toolbar>

          <android.support.design.widget.TabLayout
              android:id="@+id/tabs"
              android:layout_width="match_parent"
              android:layout_height="wrap_content"
              android:layout_gravity="bottom"
              app:tabGravity="YOUR TAB GRAVITY"
              app:tabIndicatorColor="YOUR TAB INDICATOR COLOR"
              app:tabMode="YOUR TAB MODE">

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

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

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

  <android.support.v4.view.ViewPager
  android:id="@+id/viewpager"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  app:layout_behavior="@string/appbar_scrolling_view_behavior" />

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

But now I don't want to collapse the toolbar on scroll, but on switching the tabs.

I only have to tabs and on tab 1, the toolbar should be open and on tab 2 the toolbar should be closed. And while swiping between the two tabs, the toolbar should collapse smoothly. Collapsing by scrolling should be disabled.

Is that possible?

appbarlayout.setExpanded() will help you !

tablayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
        @Override
        public void onTabSelected(TabLayout.Tab tab) {
            viewpager.setCurrentItem(tab.getPosition());
            appbar.setExpanded(false,true);
        }

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

        }

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

        }
    });

And for ViewPager :

wppager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
        @Override
        public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
          appbar.setExpanded(false,true);

        }

        @Override
        public void onPageSelected(int position) {
            appbar.setExpanded(false,true);
        }

        @Override
        public void onPageScrollStateChanged(int state) {

        }
    });

When you need to disable your CollapsingToolbarLayout :

CoordinatorLayout.LayoutParams lp = (CoordinatorLayout.LayoutParams)mAppBarLayout.getLayoutParams();
lp.height = (int) getResources().getDimension(R.dimen.toolbar_height);

Add this to dimens.xml : <dimen name="toolbar_height">48dp</dimen>

When unlocking CollapsingToolbarLayout :

CoordinatorLayout.LayoutParams lp = (CoordinatorLayout.LayoutParams)appbar.getLayoutParams();
lp.height = (int) getResources().getDimension(R.dimen.collapsing_height);
appbarlayout.setExpanded(true,true);

Add this to dimens.xml : <dimen name="collapsing_height">150dp</dimen>

The trick is here if CollapsingToolbarLayout height equals Toolbar height its stays always collapsed mode.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM