向上滚动时,折叠式工具栏中的图片消失

4
我正在尝试实现一个可折叠的工具栏,其中包含一个大的标题图片。我希望图片从非常大的尺寸开始(它可以正常工作),并且不完全折叠(也可以正常工作)。问题是当工具栏达到最小可折叠高度时,图片消失了,并淡出为应用程序的主要颜色。 我希望即使在折叠时,图片仍然可见。
此外,返回按钮与图片一起向上滚动,我希望它保持固定位置。
活动的xml文件:
    <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:fitsSystemWindows="true"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">
<android.support.design.widget.CollapsingToolbarLayout
    android:id="@+id/collapsing_toolbar"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    app:contentScrim="?attr/colorPrimary"
    app:layout_scrollFlags="scroll|exitUntilCollapsed">

    <ImageView
        android:id="@+id/imageViewToolbar"
        android:minHeight="500dp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:fitsSystemWindows="true"
        android:scaleType="centerCrop"
        app:layout_scrollFlags="scroll|enterAlways|enterAlwaysCollapsed"
        app:layout_collapseMode="parallax" />

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:title=""
        android:layout_width="match_parent"
        android:layout_height="300dp"
        app:layout_scrollFlags="scroll|enterAlways"
         />
</android.support.design.widget.CollapsingToolbarLayout>
</android.support.design.widget.AppBarLayout>
<include layout="@layout/detail_content"/>

谢谢。

3个回答

3
我已经解决了:
我通过在CollapsingToolbarLayout中添加 app:statusBarScrim="@android:color/transparent",使图像不会淡化为纯色,并通过在工具栏的布局中添加 app:layout_collapseMode="pin" 来修复了返回按钮向上滑动的问题。

1

Use this code:

<android.support.design.widget.CollapsingToolbarLayout
        android:id="@+id/collapse_toolbar"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_scrollFlags="scroll|exitUntilCollapsed"
        android:fitsSystemWindows="true">

        <ImageView
            android:id="@+id/bgheader"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:scaleType="centerCrop"
            android:fitsSystemWindows="true"
            android:background="@drawable/sunflowerpic"
            app:layout_collapseMode="pin" />

        <android.support.v7.widget.Toolbar
            android:id="@+id/MyToolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            app:layout_collapseMode="parallax" />

    </android.support.design.widget.CollapsingToolbarLayout>

在向上滚动后,输出如下。图像被固定在应用栏的背景上。

enter image description here

这是一个好的 示例

0
您可以添加一个偏移量变化侦听器,以在达到大于某个百分比时显式设置操作栏。 (有时候我不知道为什么,在具有固定高度的情况下,偏移量会随滚动行为而发生变化。) 在 Kotlin 中:
app_bar_layout.addOnOffsetChangedListener(AppBarLayout.OnOffsetChangedListener { appBarLayout, verticalOffset ->
        val percentage: Float = Math.abs(verticalOffset) / appBarLayout.totalScrollRange.toFloat()
        Log.d("%", "$percentage")
        when {
            percentage < 0.1 ->  (activity as MainActivity).bottom_navigation.visibility = View.GONE
            percentage >= 1.0.toFloat() -> (activity as MainActivity).setSupportActionBar(where_toolbar) // This is hack to avoid toolbar disappearing
            else ->  (activity as MainActivity).bottom_navigation.visibility = View.VISIBLE

        }
    })

我在这里将它用于在Fragment中膨胀。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接