CoordinatorLayout 使锚定元素在向上滚动时消失

CoordinatorLayout makes anchored element disappear when scrolled up

我有一个非常尴尬的问题,我无法摆脱,我完全不明白。

我只是想让我的应用栏折叠起来,并让图像随着应用栏区域中的某个占位符元素移动。 我从这个例子中得到了这个: https://github.com/saulmm/CoordinatorBehaviorExample

我使用了一个非常简单的布局:

<CoordinatorLayout>

    <AppBarLayout>
        <CollapsingToolbarLayout
            android:minHeight="96dp"
            app:layout_scrollFlags="exitUntilCollapsed|scroll">
            <FrameLayout android:id="@+id/anchorPlaceHolder .../>
        </CollapsingToolbarLayout>
    </AppBarLayout>

    <NestedScrollView ...>

    <ImageView
        app:layout_anchor="@id/anchorPlaceHolder
        ...
        />

</CoordinatorLayout>

一切都按预期工作 - 除了一个大错误。 当滚动到达它的顶端时——也就是工具栏达到它的最小高度——我的图像就消失了。这是 "switched off" - 我不知道为什么以及谁在这样做。

我检查了层次结构,但可见性仍然 "VISIBLE",没有关闭或其他任何东西。

谁能解释一下这是怎么回事?

好的,我终于找到了这个问题的答案。

它与 AppBarLayout 的原本看不到的高度有关,它由包含的 CollapsingToolbarLayout 修改,以防万一其中有固定元素,只有那些在顶部仍然可见。

意思是:"As long as we scroll out with the content, we are inline with the content. Once the app bar stops (only pinned elements left), we lift up and let the remaining content slide underneath."

仅当您要显示的所有元素实际上都是 CollapsingToolbarLayout 的子元素时,此行为才整洁,但在我的例子中,我想锚定在 CoordinatorLayout 中自由浮动的元素作为兄弟姐妹带有一些工具栏元素的应用栏。但是,一旦我们达到固定状态,应用栏就会提升 在那些自由浮动且仍然锚定的元素的顶部 - 从而隐藏它们。

一个解决方案:给这些元素更高的高度。 :-)

或者:将 AppBarLayout 的高度设置为 0dp。

我没有找到这个的原因很简单:包含的 CollapsingToolbarLayout 会影响父级的高度...;-)