我的折叠工具栏的xml代码是,
<android.support.design.widget.CollapsingToolbarLayout
android:id="@+id/collapsing_toolbar"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:expandedTitleGravity="left"
app:expandedTitleMarginTop="-10dp"
app:expandedTitleMarginStart="10dp"
app:contentScrim="?attr/colorPrimary"
app:layout_scrollFlags="scroll|exitUntilCollapsed">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="130dp">
<ImageView
android:id="@+id/ivCImage"
android:layout_width="match_parent"
android:layout_height="130dp"
android:scaleType="fitXY"
android:src="@drawable/image_place_holder" />
</RelativeLayout>
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
app:layout_collapseMode="pin">
</android.support.v7.widget.Toolbar>
</android.support.design.widget.CollapsingToolbarLayout>
</android.support.design.widget.AppBarLayout>
我在编码方面所做的是,
private CollapsingToolbarLayout collapsingToolbarLayout = null;
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
ActionBar actionBar = getSupportActionBar();
actionBar.setDisplayHomeAsUpEnabled(true);
collapsingToolbarLayout = (CollapsingToolbarLayout) findViewById(R.id.collapsing_toolbar);
它总是应该显示后退图标但我想在折叠CollapsingToolbarLayout时显示/启用主页(后退)图标。我怎样才能做到这一点? 任何帮助/建议将不胜感激。
答案 0 :(得分:0)
首先创建一个标志
boolean FLAG_COLLAPSED = true; //Change this according to your view
然后在AppBar中添加一个监听器
AppBarLayout appBarLayout = (AppBarLayout) findViewById(R.id.AppBarLayout);
appBarLayout.addOnOffsetChangedListener(new AppBarLayout.OnOffsetChangedListener() {
@Override
public void onOffsetChanged(AppBarLayout appBarLayout, int verticalOffset) {
if (Math.abs(verticalOffset) == appBarLayout.getTotalScrollRange()) {
// Collapsed
FLAG_COLLAPSED = true;
} else if (verticalOffset == 0) {
FLAG_COLLAPSED = false;
} else {
// Somewhere in between
}
}
}););
现在在你的背压方法中,
@Override
public void onBackPressed() {
if(FLAG_COLLAPSED){
super.onBackPressed();
}
}
答案 1 :(得分:0)
是的,我已经轻松完成了这项工作,您必须在工具栏中启用后退导航,与折叠工具栏无关,并且在清单文件中告诉您的活动其父类,作为书面父活动名称。
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setDisplayShowHomeEnabled(true);
initCollapsingToolbar();
崩溃时:
private void initCollapsingToolbar() {
final CollapsingToolbarLayout collapsingToolbar =
(CollapsingToolbarLayout) findViewById(R.id.collapsing_toolbar);
collapsingToolbar.setTitle(" ");
AppBarLayout appBarLayout = (AppBarLayout) findViewById(R.id.appbar);
appBarLayout.setExpanded(true);
appBarLayout.addOnOffsetChangedListener(new AppBarLayout.OnOffsetChangedListener() {
boolean isShow = false;
int scrollRange = -1;
@Override
public void onOffsetChanged(AppBarLayout appBarLayout, int verticalOffset) {
if (scrollRange == -1) {
scrollRange = appBarLayout.getTotalScrollRange();
}
if (scrollRange + verticalOffset == 0) {
collapsingToolbar.setTitle(getString(R.string.Yoga_tips));
isShow = true;
} else if (isShow) {
collapsingToolbar.setTitle(" ");
isShow = false;
}
}
});
}