5

The classic coordinator layout gives you the following [source]: coordinator layout

However, I don't want the top header views to scroll until they "become" a toolbar pinned at the top, with a shadow below. I want them all fixed (or pinned) but to show the shadow only the nested scroll view starts to scroll under the pinned ones. Something like the main app drawer on Marshmallow devices, where the "search bar" becomes pinned and the list of apps scroll under it.

Hope I made myself clear. Is there any easy way I could achieve that without listening for scroll events and handling this manually?

EDIT

Here is what I'm trying to achieve:

App drawer. Notice on the right image how there is now a shadow below the list of apps because the user scrolled the list.

Thank you!

Community
  • 1
  • 1
Testing Here
  • 185
  • 1
  • 9
  • Not quite sure what you are asking for. What you are displaying in your post is the `CollapsingToolbarLayout`. The collapsing behavior is distinct from the scrolling behavior. The `AppBarLayout` by itself can scroll the toolbar while pinning other app bar views. Does that help? Maybe you could post some diagrams of how you want your app to look before and after scrolling. – kris larson Jun 27 '16 at 14:55
  • @krislarson Thanks! I added an example from Marshmallow's app drawer. Notice how first there is no shadow and then it appears as the user scrolls. I want the same for a toolbar + a linear layout with children views inside. Setting `app:layout_collapseMode="pin"` on the views did not work for me. :( – Testing Here Jun 27 '16 at 15:12
  • You should be able to do this with `CollapsingToolbarLayout`. Can you post your layout XML? I did find out that the latest support library is using a state list animator to control elevation for `AppBarLayout`, see this answer: http://stackoverflow.com/a/32393698/4504191 – kris larson Jun 27 '16 at 20:00
  • @krislarson thanks. I don't to collapse the toolbar and did not find an option to fix (or pin) the toolbar and the other views I want fixed on top. Also, I don't have a problem to either set the shadow to always appear or always hide using the elevation. What I don't get is how to make it appear only when I scroll under the fixed views. – Testing Here Jun 28 '16 at 07:40
  • 1
    Did you found a solution for this? – Bri6ko Apr 25 '18 at 18:27

2 Answers2

2

This is exactly what you are looking for: HideOnScroll

RATHI
  • 4,551
  • 7
  • 34
  • 44
  • 1
    This gist and the blog post is amazing. But it uses `AppBarLayout.OnOffsetChangeListener` which means you need a scrolling AppBar in order to use that technique. If you have a fixed AppBar/Toolbar that wouldn't work. – tasomaniac Aug 10 '17 at 14:58
  • 1
    Please post relevant information from the link as link only answers are discouraged – Spikatrix Feb 25 '20 at 06:22
0

Set actionBar elevation to 0 initially. Add a scroll listener to your scrolling element. When you detect it has scrolled (dy > 0) you set the actionBar elevation to 4dp(the default actionBar elevation) and back to 0dp at dy == 0.

In your scroll listener you can, at least for recyclerViews, use the canScrollVertically function to check if you're at the top or not.

hordurh
  • 2,653
  • 1
  • 14
  • 17