问题
I have a really awkward problem that I cannot get rid of and that I absolutely do not understand.
I simply want my app bar to collapse and an image to move with some placeholder element in the app bar area. I got this from this example: https://github.com/saulmm/CoordinatorBehaviorExample
I use a very simple layout:
<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>
Everything works just as expected - EXCEPT for one massive bug. When the scrolling reaches its top end - aka the toolbar reaches its minimum height - my image simply disappears. It's "switched off" - and I don't know why and who's doing this.
I inspected the hierachy, but visibility is still "VISIBLE", not switched off or anything.
Can anybody explain what is going on here?
回答1:
Okay, I finally found my answer to this question.
It has to do with the otherwise not seen elevation of the AppBarLayout
which is modified by the contained CollapsingToolbarLayout
just in case there are pinned elements within and only those are still visible on top.
It's meant to say: "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."
This behavior is neat only if all elements that you want to show are actually children of the CollapsingToolbarLayout
, but in my case I wanted to anchor elements which freely float within the CoordinatorLayout
as siblings of the app bar with some toolbar elements. But as soon as we reach the pinned state, the app bar is lifted on top of those free floating and still anchored elements - thus hiding them.
One solution: give those elements an even higher elevation. :-)
Or: set the elevation of the AppBarLayout
to 0dp.
The reason I did not find this is simple: the contained CollapsingToolbarLayout
fiddles with the parent's elevation... ;-)
来源:https://stackoverflow.com/questions/39913809/coordinatorlayout-makes-anchored-element-disappear-when-scrolled-up