Need to disable expand on CollapsingToolbarLayout for certain fragments

前端 未结 14 1298
萌比男神i
萌比男神i 2020-11-28 08:55

I have a AppCompatActivity that controls replacing many fragments. Here is my layout for it.

activity_main.xml



        
相关标签:
14条回答
  • 2020-11-28 09:49

    You can lock appbarlayout expansion by resetting collapsing toolbar height to toolbar height

    toolbarHeight = toolbar.getLayoutParams().height;
    
    if (expand) {
        collapsingToolbar.getLayoutParams().height = getResources().getDimensionPixelOffset(R.dimen.collapsingToolbarDefaultHeight);
        appBarLayout.setExpanded(true, true);
    } else {
        //collapse
        //** it is important you do this before resetting **
        appBarLayout.setExpanded(false, true);
        appBarLayout.postDelayed(new Runnable() {
            @Override
            public void run() {
                collapsingToolbar.getLayoutParams().height = toolbarHeight;
            }
         }, 700/* 600 is default animation time to collapse */);
    }
    
    0 讨论(0)
  • 2020-11-28 09:53

    I have found a workaround solution that works with activity and various fragments. You implement the CollapsingToolbarLayout with AppBar etc in your activity and then each time you call a new fragment you can call these 2 functions.

    • When I want my appbar to keep collapsed:

      public void lockAppBarClosed() {
          mAppBarLayout.setExpanded(false, false);
          mAppBarLayout.setActivated(false);
          CoordinatorLayout.LayoutParams lp = (CoordinatorLayout.LayoutParams)mAppBarLayout.getLayoutParams();
          lp.height = (int) getResources().getDimension(R.dimen.toolbar_height);
      }
      
    • When I want my appbar to be expanded and scrollable again

      public void unlockAppBarOpen() {
          mAppBarLayout.setExpanded(true, false);
          mAppBarLayout.setActivated(true);
          CoordinatorLayout.LayoutParams lp = (CoordinatorLayout.LayoutParams)mAppBarLayout.getLayoutParams();
          lp.height = (int) getResources().getDimension(R.dimen.toolbar_expand_height);
      }
      

    You can call thoses functions from your fragments by implementing an interface. Here's a quick example, for your case (toolbar expands only in homeFragment)

    public interface CustomListener() {
        void unlockAppBarOpen();
        void lockAppBarClosed()
    }
    
    public class MainActivity extends BaseActivity implements CustomListener {
    
        @Override
        public void unlockAppBarOpen() {
            mAppBarLayout.setExpanded(true, false);
            mAppBarLayout.setActivated(true);
            CoordinatorLayout.LayoutParams lp = (CoordinatorLayout.LayoutParams)mAppBarLayout.getLayoutParams();
            lp.height = (int) getResources().getDimension(R.dimen.toolbar_expand_height);
        }
    
        @Override
        public void lockAppBarClosed() {
            mAppBarLayout.setExpanded(false, false);
            mAppBarLayout.setActivated(false);
            CoordinatorLayout.LayoutParams lp = (CoordinatorLayout.LayoutParams)mAppBarLayout.getLayoutParams();
            lp.height = (int) getResources().getDimension(R.dimen.toolbar_height);
        }
    }
    
    public class MainFragment extends BaseFragment {
    
        @Override
        public void onResume() {
            super.onPause();
            ((MainActivity) getContext()).unlockAppBarOpen();
        }
    }
    
    public class SecondFragment extends BaseFragment {
    
        @Override
        public void onResume() {
            super.onPause();
            ((MainActivity) getContext()).lockAppBarClosed();
        }
    }
    

    With this example:

    • each time the MainFragment will be displayed -> it will extends the toolbar and make it collapsable and expandable

    • each time the SecondFragment is displayed -> il will collapsed the toolbar to a standard size and prevent it to expand again

    Hope it will help you !

    0 讨论(0)
提交回复
热议问题