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

1

0

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.

Yoko answered 10/5, 2018 at 14:25 Comment(0)
Y
0

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.

Yoko answered 11/5, 2018 at 2:6 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.