Auto-hiding toolbar on scroll, when there are several other layers of views in-between

坚强是说给别人听的谎言 提交于 2019-12-11 14:33:53

问题


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.

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

The seemingly redundant two LinearLayouts 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|
+------------+

which should then scroll to

+TOOLBAR----=+
+------------+
|            |
|   IMAGE    |
|            |
|111111111111| 
|222222222222|
|333333333333|
|444444444444|
|555555555555|
+------------+

and further to

+------------+
|            |
|   IMAGE    |
|            |
|111111111111| 
|222222222222|
|333333333333|
|444444444444|
|555555555555|
|666666666666|
+------------+

and then to

+------------+
|            |
|   IMAGE    |
|            |
|222222222222|
|333333333333|
|444444444444|
|555555555555|
|666666666666|
|777777777777| 
+------------+

(with the numbers representing the things in the ScrollView).

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.


回答1:


It turns out I need to use NestedScrollView, since, unlike its name suggested to me, it is not just for nesting ScrollViews into other ScrollViews, but also for nesting a ScrollView inside other containers:

NestedScrollView is just like ScrollView, but it supports acting as both a nested scrolling parent and child on both new and old versions of Android.



来源:https://stackoverflow.com/questions/50275244/auto-hiding-toolbar-on-scroll-when-there-are-several-other-layers-of-views-in-b

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!