【发布时间】:2016-06-02 04:37:39
【问题描述】:
我正试图阻止 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);
【问题讨论】:
-
你能解释一下px代码行中的
80是什么吗,因为它给了我px初始化行,FATAL EXCEPTION: main Process: project...n, PID: 24890 java.lang.IllegalStateException: Fragment xFragment{ffd3ec0} not attached to Activity at android.support.v4.app.Fragment.getResources(Fragment.java:646) at project....Fragment.<init> -
@DasserBasyouni 听起来您遇到了另一个问题,即片段未附加到活动中。尝试搜索
not attached to Activity,例如stackoverflow.com/questions/25262994/…
标签: android-studio android-toolbar android-appbarlayout appbar android-collapsingtoolbarlayout