Need to disable expand on CollapsingToolbarLayout for certain fragments

前端 未结 14 1303
萌比男神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: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 !

提交回复
热议问题