Need to disable expand on CollapsingToolbarLayout for certain fragments

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

    None of the provided solutions worked for me except this one. With this solution, i can easily manage the state of collapsing toolbar. This will prevent expanding of collapsing toolbar and set title for it.

    public void lockAppBar(boolean locked,String title) {
        if(locked){
            appBarLayout.setExpanded(false, true);
            int px = (int)TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 80, getResources().getDisplayMetrics());
            CoordinatorLayout.LayoutParams lp = (CoordinatorLayout.LayoutParams)appBarLayout.getLayoutParams();
            lp.height = px;
            appBarLayout.setLayoutParams(lp);
            collapsingToolbarLayout.setTitleEnabled(false);
            toolbar.setTitle(title);
        }else{
            appBarLayout.setExpanded(true, false);
            appBarLayout.setActivated(true);
            CoordinatorLayout.LayoutParams lp = (CoordinatorLayout.LayoutParams) appBarLayout.getLayoutParams();
            lp.height = (int) getResources().getDimension(R.dimen.toolbarExpandHeight);
            collapsingToolbarLayout.setTitleEnabled(true);
            collapsingToolbarLayout.setTitle(title);
        }
    }
    

提交回复
热议问题