我有一个片段,我想在其中使用折叠工具栏布局
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true">
<android.support.design.widget.AppBarLayout
android:id="@+id/app_bar_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
android:fitsSystemWindows="true">
<android.support.design.widget.CollapsingToolbarLayout
android:id="@+id/collapsing_toolbar"
android:layout_width="match_parent"
android:layout_height="350dp"
app:layout_scrollFlags="scroll|exitUntilCollapsed"
app:contentScrim="?attr/colorPrimary"
app:expandedTitleTextAppearance="@android:color/transparent"
android:fitsSystemWindows="true">
<ImageView
android:id="@+id/header_image"
android:layout_width="match_parent"
android:layout_height="350dp"
android:scaleType="centerCrop"
android:fitsSystemWindows="true"
android:contentDescription="@string/app_name"
android:src="@drawable/festival"
app:layout_collapseMode="parallax"/>
<include layout="@+id/custom_layout"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize" />
</android.support.design.widget.CollapsingToolbarLayout>
</android.support.design.widget.AppBarLayout>
<android.support.v4.widget.NestedScrollView
//...custom view /> ...
当collapsing_toolbar展开时,我希望显示图像,当折叠时,我希望只有@id/custom_layout。custom_layout是一个带有文本视图和图像视图的相对布局。
我想要有完全相同的行为,就像我有以下行为一样:
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
app:layout_collapseMode="pin" />
而不是自定义布局。
为什么这是不工作?即使折叠ToobarLayout扩展我看到的ImageView和自定义布局。
!!注意我确实有一个定义了工具栏的活动。我不想碰这部分代码。当我向上滚动片段时,我希望@id/custom_layout在活动中定义的现有工具栏下面对齐。
我在片段中添加了以下onViewCreated()方法:
RelativeLayout headerLayout = view.findViewById(R.id.custom_layout);
AppBarLayout mAppBarLayout = view.findViewById(R.id.app_bar_layout);
mAppBarLayout.addOnOffsetChangedListener(new
AppBarLayout.OnOffsetChangedListener() {
@Override
public void onOffsetChanged(AppBarLayout appBarLayout, int verticalOffset) {
if (verticalOffset == 0) {
//fully expanded
headerLayout.setVisibility(View.GONE)
} else {
//fully collapsed
headerLayout.setVisibility(View.Visible);
//ISSUE HERE!!!: Only when ImageView has height = 0, the headerLayout pops up.
//!!The transition is not smoothly.
// I would like the headerLayout to be visible when the ImageView height reaches the headerLayout height.
}
}
});
当折叠工具栏
折叠时,您可以隐藏ImageView
,并在展开时再次显示。在您的活动类中,使用onOffsetChangedListener
:
AppBarLayout appBarLayout = (AppBarLayout) view.findViewById(R.id.app_bar_layout);
appBarLayout.addOnOffsetChangedListener(new AppBarLayout.OnOffsetChangedListener() {
@Override
public void onOffsetChanged(AppBarLayout appBarLayout, int verticalOffset) {
if (Math.abs(verticalOffset) == appBarLayout.getTotalScrollRange()) {
// If collapsed, then do this
imageViewHeaderImage.setVisibility(View.GONE);
} else if (verticalOffset == 0) {
// If expanded, then do this
imageViewHeaderImage.setVisibility(View.VISIBLE);
}
}
}););
你可以按程序来做。在活动中,在OnCreate()
方法中添加此侦听器
ImageView headerImage = view.findViewById(R.id.header_image);
AppBarLayout mAppBarLayout = view.findViewById(R.id.app_bar_layout);
mAppBarLayout.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) {
isShow = true;
headerImage.setVisibility(View.VISIBLE);
} else if (isShow) {
isShow = false;
headerImage.setVisibility(View.GONE);
}
}
});
编辑为什么你不能得到与实际工具栏相同的效果
docs statecollazingtoolbarlayout
是工具栏的包装器,它实现了一个折叠的应用程序栏。它被设计成一个AppBarLayout
的直接子级。所以它被谷歌设计成与工具栏一起使用。您可以创建一些简单的变通方法来使用自定义布局
我正在使用以及和设置为固定,我想知道当折叠时,是否有方法更改工具栏的标题文本。 总结一下,我想要滚动和展开时的两个不同的标题。 提前谢谢大家
我知道有成千上万个这样的问题,但是我尝试了所有的方法,但是我不能想出一个解决方案。基本上,我使用的是NoActionBar风格的活动。 风格。xml: v21/风格。xml: 家庭活动正常,抽屉将在透明状态栏下正常打开: 问题是使用以下布局的另一个活动: 基本上,状态栏是透明的,所以我看不到它,因为我的colorPrimary是白色的。奇怪的是,如果我折叠工具栏(因此只有选项卡可见),状态栏将正确
我已经实现了一个带有视差视图的。 当我在一次连续滚动中向上滚动回收站视图时,它会展开。 但是当我对嵌套的滚动视图发出“fling”命令时,它停止在嵌套的滚动视图的顶部。您必须再次投掷才能取消折叠< code > CollapsingToolbarLayout , Android Studio 2.0中的ScrollingActivity模板演示了这个问题。克里斯·贝恩(Chris Bain)的奶酪
再次强调,在绑定的ViewHolder中,如果我开始在嵌套的RecycerView之外的任何其他项上滚动,滚动工作很好。但是,如果我在嵌套的RecycerView上开始滚动,那么它就会中断。 是否可以防止内部RecycerView上的滚动拦截,只让父级滚动?但是,我希望内部的RecycerView仍然可以处理点击。 以下是关于这种现象的YouTube链接:https://youtu.be/fzj7
这个链接!告诉我如何禁用折叠工具栏布局。我想要的行为是折叠并禁用折叠工具栏,并在没有internet连接时显示错误视图。那么我如何才能做到这一点,折叠和禁用折叠工具栏布局?
我有一个折叠工具栏布局设置,我在那里放置壁纸。我希望能够阻止它一路崩溃。 我尝试过minheight和许多其他事情,但无法弄清楚。 怎么才能让它停止折叠到第二张截图? 加载活动时查看 期望的停止点 当前停止点