滚动时自动隐藏工具栏,当中间有其他几层视图时 [英] Auto-hiding toolbar on scroll, when there are several other layers of views in-between
问题描述
我希望我的活动的Toolbar
随着给定的scrollview的增长而平滑地滚动出,然后如果用户向下滚动则平滑地向后滚动. IE.基本的scroll|enterAlways
行为,例如此答案.
I would like my activity's Toolbar
to smoothly scroll out as a given scrollview grows, and then smoothly scroll back in if the user scrolls down. I.e. the basic scroll|enterAlways
behaviour from e.g. this answer.
但是,我的布局在包含Toolbar
的CoordinatorLayout
和应该驱动所有内容的ScrollView
之间有一些层.与此答案不同,我希望采用以下布局:
However, my layout has some layers between the CoordinatorLayout
containing the Toolbar
, and the ScrollView
that should be driving everything. Unlike in this answer, I'd like to have the following layout:
CoordinatorLayout
AppBarLayout
Toolbar
LinearLayout [vertical]
LinearLayout [vertical]
ImageView
ScrollView
看似多余的两个LinearLayout
用于控制背景色;而ImageView
和ScrollView
的想法是我的UI应该如下所示:
The seemingly redundant two LinearLayout
s are for background color control; and the idea with the ImageView
and the ScrollView
is that my UI should look like this:
+------------+
|TOOLBAR =|
+------------+
| |
| IMAGE |
| |
|111111111111|
|222222222222|
|333333333333|
|444444444444|
+------------+
然后应滚动到
+TOOLBAR----=+
+------------+
| |
| IMAGE |
| |
|111111111111|
|222222222222|
|333333333333|
|444444444444|
|555555555555|
+------------+
并进一步
+------------+
| |
| IMAGE |
| |
|111111111111|
|222222222222|
|333333333333|
|444444444444|
|555555555555|
|666666666666|
+------------+
然后到
+------------+
| |
| IMAGE |
| |
|222222222222|
|333333333333|
|444444444444|
|555555555555|
|666666666666|
|777777777777|
+------------+
(数字代表ScrollView
中的内容).
(with the numbers representing the things in the ScrollView
).
仅通过设置即可获得上述行为
Toolbar
上的app:layout_scrollFlags="scroll|enterAlways"
作为标准.但是,如果我随后开始在ScrollView
上向下拖动,则Toolbar
不会回来.实际上,一旦Toolbar
滚动出来,我还没有找到任何方法来取回它.
The above behaviour I was able to get just by setting
app:layout_scrollFlags="scroll|enterAlways"
on the Toolbar
as standard. However, if I then start dragging down on the ScrollView
, the Toolbar
doesn't come back. In fact, I haven't found any way to get the Toolbar
back once it has scrolled out.
推荐答案
It turns out I need to use NestedScrollView
, since, unlike its name suggested to me, it is not just for nesting ScrollView
s into other ScrollView
s, but also for nesting a ScrollView
inside other containers:
NestedScrollView
就像ScrollView
一样,但是它支持表演 作为新老版本Android上的嵌套滚动父级和子级.
NestedScrollView
is just likeScrollView
, but it supports acting as both a nested scrolling parent and child on both new and old versions of Android.
这篇关于滚动时自动隐藏工具栏,当中间有其他几层视图时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!