我试图防止CollapsingToolbar
扩展为我的片段之一。
当前,当我使用方法setExpanded()
时,可以看到工具栏处于折叠状态。
appBarLayout.setExpanded(false, true);
但是,collapsingToolbar仍可扩展,这不是我想要的。我希望collapsingToolbar对于此特定片段不可扩展。
换句话说,我希望collapsingToolbar的行为和外观仅像针对此特定片段的常规工具栏(即“不可扩展”)一样。
我正在使用Mike Penz's Material Drawer。下面的代码显示了相关的代码,并带有说明我尝试过的内容。
private void buildDrawer(){
Drawer drawer = new DrawerBuilder()
.withActivity(this)
.withFullscreen(true)
.withTranslucentStatusBar(true)
.withToolbar(toolbar)
.withAccountHeader(accountHeader)
.addDrawerItems(
item1,
item2,
new DividerDrawerItem(),
item3,
item4
)
.withOnDrawerItemClickListener(new Drawer.OnDrawerItemClickListener() {
@Override
public boolean onItemClick(View view, int position, IDrawerItem drawerItem) {
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
Fragment f = new Fragment();
switch (position) {
//other cases not shown
case 2:
f = new LocateFragment();
appBarLayout.setExpanded(false, true);
//Adding the following three lines don't work - causes the toolbar to be unscrollable, but in its expanded form
//AppBarLayout.LayoutParams p = (AppBarLayout.LayoutParams)collapsingToolbarLayout.getLayoutParams();
//p.setScrollFlags(AppBarLayout.LayoutParams.SCROLL_FLAG_SNAP);
//collapsingToolbarLayout.setLayoutParams(p);
//toolbar.setCollapsible(false); doesn't work either
collapsingToolbarLayout.setTitle("Locate Events");
setInvisibleAddPhotoFab();
break;
//other cases not shown
}
fragmentTransaction.replace(R.id.frame_fragments, f);
fragmentTransaction.commit();
return false; //close drawer onclick
}
})
.build();
loadBackdrop();
}
这就是我想要的-我希望工具栏无法展开:
但是,当前它仍然可以扩展,因此下面的图像不是我想要的:
更新:
我能够折叠工具栏并阻止它像这样展开(下面的代码),但是我遇到了另一个问题-当我这样设置它时,标题不再出现在collapsingToolbar上:
collapsingToolbarLayout.setTitle("string");
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);
最佳答案
我可以使用以下方法解决此问题(如@DanielPersson的here所示):
collapsingToolbarLayout.setTitleEnabled(false);
toolbar.setTitle(title);