<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">

 <title>Michał Z.</title>
 <link href="http://mzgreen.github.io/atom.xml" rel="self"/>
 <link href="http://mzgreen.github.io/"/>
 <updated>2015-09-25T08:11:47+00:00</updated>
 <id>http://mzgreen.github.io</id>
 <author>
   <name>Michał Z.</name>
   <email></email>
 </author>

 
 <entry>
   <title>How to hide/show Toolbar when list is scrolling (part 3)</title>
   <link href="http://mzgreen.github.io//2015/06/23/How-to-hideshow-Toolbar-when-list-is-scrolling(part3)/"/>
   <updated>2015-06-23T00:00:00+00:00</updated>
   <id>http://mzgreen.github.io/2015/06/23/How-to-hideshow-Toolbar-when-list-is-scrolling(part3)</id>
   <content type="html">&lt;p&gt;This is a third (and the really last) post of this series :) If you haven’t read &lt;a href=&quot;http://mzgreen.github.io/2015/02/15/How-to-hideshow-Toolbar-when-list-is-scroling%28part1%29/&quot;&gt;part 1&lt;/a&gt; or &lt;a href=&quot;https://mzgreen.github.io/2015/02/28/How-to-hideshow-Toolbar-when-list-is-scrolling%28part2%29/&quot;&gt;part 2&lt;/a&gt; that’s ok because they are deprecated now. In previous parts I showed how to manually achieve hiding/showing Toolbar when list is scrolling. But now we have the &lt;a href=&quot;http://android-developers.blogspot.com/2015/05/android-design-support-library.html&quot;&gt;Design Support Library&lt;/a&gt; and we don’t have to write it on our own! I will show how to use &lt;a href=&quot;http://developer.android.com/reference/android/support/design/widget/CoordinatorLayout.html&quot;&gt;CoordinatorLayout&lt;/a&gt; and create a simple &lt;a href=&quot;http://developer.android.com/reference/android/support/design/widget/CoordinatorLayout.Behavior.html&quot;&gt;Behavior&lt;/a&gt; to hide/show Toolbar and FAB when list scrolls up/down. Many of you had a problem with tabs and lists that contain too few items to scroll. I’m happy to tell you that thanks to &lt;a href=&quot;http://developer.android.com/reference/android/support/design/widget/CoordinatorLayout.html&quot;&gt;CoordinatorLayout&lt;/a&gt; all these problems are gone :) Let’s begin!&lt;/p&gt;

&lt;p&gt;This is what we want to achieve:
&lt;img src=&quot;/images/3/scrollingtoolbarandfab.gif&quot; alt=&quot;Working example gif&quot; title=&quot;Working example gif&quot; /&gt;&lt;/p&gt;

&lt;h2 id=&quot;preparation&quot;&gt;Preparation&lt;/h2&gt;
&lt;p&gt;In order to use the new cool features that &lt;a href=&quot;http://android-developers.blogspot.com/2015/05/android-design-support-library.html&quot;&gt;Design Support Library&lt;/a&gt; has to offer we need to modify our &lt;code&gt;build.gradle&lt;/code&gt; file:
&lt;script src=&quot;https://gist.github.com//mzgreen/233f4f7da7142a0e3fc2.js&quot;&gt; &lt;/script&gt;&lt;/p&gt;

&lt;p&gt;It’s just updated SDK, build tools and libraries to the newest version. And as you can see there is also a new one at the end &lt;code&gt;compile &#39;com.android.support:design:22.2.0&#39;&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Now we can create our &lt;code&gt;PartThreeActivity&lt;/code&gt; and fill it with some data. Our demo will have 2 tabs with lists of items on each tab. So we’ll need a &lt;code&gt;ViewPager&lt;/code&gt; which will be showing two &lt;code&gt;Fragments&lt;/code&gt;. In our case both &lt;code&gt;Fragments&lt;/code&gt; will display a list of items, so they will be the same. The only difference is that we will show 20 items on first page and only 5 items on second page. This is because I want to show you that even if list is too short to scroll we will be able to show/hide the &lt;code&gt;Toolbar&lt;/code&gt; and FAB. So let’s start from creating a &lt;code&gt;PartThreeFragment&lt;/code&gt; class.
Here is it’s layout file:
&lt;script src=&quot;https://gist.github.com//mzgreen/fd6e50f3391f6eddf56c.js&quot;&gt; &lt;/script&gt;&lt;/p&gt;

&lt;p&gt;Nothing special here, just a &lt;code&gt;RecyclerView&lt;/code&gt;. Let’s jump to &lt;code&gt;PartThreeFragment&lt;/code&gt; code:
&lt;script src=&quot;https://gist.github.com//mzgreen/2ad432f2097b151cf0cd.js&quot;&gt; &lt;/script&gt;&lt;/p&gt;

&lt;p&gt;It’s an ordinary &lt;code&gt;Fragment&lt;/code&gt; so I won’t describe it. The only interesting part is that we can give it a number of items that our list will contain (look at &lt;code&gt;createInstance(int itemsCount)&lt;/code&gt; and &lt;code&gt;createItemList()&lt;/code&gt; methods). &lt;code&gt;RecyclerAdapter&lt;/code&gt; and the layout of a single list item is exactly the same as we created in &lt;a href=&quot;https://mzgreen.github.io/2015/02/28/How-to-hideshow-Toolbar-when-list-is-scrolling%28part2%29/&quot;&gt;part 2&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;So we have it, now we need to setup our &lt;code&gt;PartThreeActivity&lt;/code&gt;. Let’s look at the layout file:
&lt;script src=&quot;https://gist.github.com//mzgreen/aeb61d9a4d9cb0e079a1.js&quot;&gt; &lt;/script&gt;&lt;/p&gt;

&lt;p&gt;Let’s stop a little bit. There are a few new things in there. First is the &lt;a href=&quot;http://developer.android.com/reference/android/support/design/widget/CoordinatorLayout.html&quot;&gt;CoordinatorLayout&lt;/a&gt;. It’s a new layout that let’s you create interactions between &lt;code&gt;Views&lt;/code&gt; that you put inside it. As the documentation says it’s a &lt;code&gt;FrameLayout&lt;/code&gt; on steroids. It intercepts touch events and let’s you inject your own [Behaviors]. We’ll see an example of this a little bit later. Next new thing is the [AppBarLayout]. It’s an enhanced version of &lt;code&gt;Linearlayout&lt;/code&gt; that allows you to specify scrolling interactions. You can for example create a collapsing &lt;code&gt;Toolbar&lt;/code&gt; effect or some other things with it. It’s as simple as puttng it into your layout and setting some attributes. But it’s designed to work in pair with &lt;code&gt;CoordinatorLayout&lt;/code&gt; so if you use it in some other &lt;code&gt;ViewGroup&lt;/code&gt; you will loose it’s functionality. The last new thing that you can see here is the &lt;code&gt;TabLayout&lt;/code&gt; which i think is self-explanatory. It’s basically a new material version of tabs for Android. In our case we want to have the &lt;code&gt;Tabs&lt;/code&gt; below the &lt;code&gt;Toolbar&lt;/code&gt; so we wrap them in an &lt;code&gt;AppBarLayout&lt;/code&gt;. The last part of our layout is a &lt;code&gt;ViewPager&lt;/code&gt;. Let’s glue it all together using some Java code:
&lt;script src=&quot;https://gist.github.com//mzgreen/65a3ae848d8fb756a65f.js&quot;&gt; &lt;/script&gt;&lt;/p&gt;

&lt;p&gt;The only interesting fragment of this class is &lt;code&gt;ViewPager&lt;/code&gt; and &lt;code&gt;TabLayout&lt;/code&gt; initialization which you can see above. &lt;code&gt;PagerAdapter&lt;/code&gt; is a simple implementation of &lt;code&gt;FragmentPagerAdapter&lt;/code&gt; so I assume that you are familiar with it. In lines 4 and 5 we are adding two pages to our adapter. First page will contain our &lt;code&gt;PartThreeFragment&lt;/code&gt; with 20 items and second page will contain the same &lt;code&gt;Fragment&lt;/code&gt; but with only 5 items.&lt;/p&gt;

&lt;p&gt;We can now compile it and see how it looks like:
&lt;img src=&quot;/images/3/screencuttop.png&quot; alt=&quot;Cut top&quot; title=&quot;Cut top&quot; /&gt;&lt;/p&gt;

&lt;p&gt;It works but as you can see, the top of the list is not fully visible. It seems that it’s partially covered by our &lt;code&gt;AppBarLayout&lt;/code&gt;. To fix this we need to add a &lt;code&gt;Behavior&lt;/code&gt; to a &lt;code&gt;ViewPager&lt;/code&gt;. It looks like this:
&lt;script src=&quot;https://gist.github.com//mzgreen/cb75d3f8bf665d6c4725.js&quot;&gt; &lt;/script&gt;&lt;/p&gt;

&lt;p&gt;This string value points to &lt;code&gt;android.support.design.widget.AppBarLayout$ScrollingViewBehavior&lt;/code&gt; so it’s a fully qualifed &lt;a href=&quot;http://developer.android.com/reference/android/support/design/widget/CoordinatorLayout.Behavior.html&quot;&gt;Behavior&lt;/a&gt; class name. It’s a static inner class inside &lt;code&gt;AppBarLayout&lt;/code&gt; class. &lt;code&gt;Behavior&lt;/code&gt; is a mechanism that let’s you specify interactions between the &lt;code&gt;Views&lt;/code&gt; that you put inside a &lt;code&gt;CoordinatorLayout&lt;/code&gt;. The &lt;code&gt;CoordinatorLayout&lt;/code&gt; parses &lt;code&gt;Behaviors&lt;/code&gt; and handles interactions between &lt;code&gt;Views&lt;/code&gt;. Thanks to the fact that you can add it using xml attribute allows adding a &lt;code&gt;Behavior&lt;/code&gt; to any &lt;code&gt;View&lt;/code&gt;. This is a very powerful API and I really encourage you to play with it. In our case adding &lt;code&gt;ScrollingViewBehavior&lt;/code&gt; to the &lt;code&gt;ViewPager&lt;/code&gt; causes that it will be positioned correctly according to &lt;code&gt;AppBarLayout&lt;/code&gt; and its position will be updated as &lt;code&gt;AppBarLayout&lt;/code&gt; scrolls. The new &lt;a href=&quot;http://android-developers.blogspot.com/2015/05/android-design-support-library.html&quot;&gt;Design Support Library&lt;/a&gt; source is not available yet but you can use Android Studio built in decompiler to see what exactly &lt;code&gt;ScrollingViewBehavior&lt;/code&gt; is doing.
If we run our app now we should see something like this:
&lt;img src=&quot;/images/3/onlytabs.gif&quot; alt=&quot;Working tabs and list&quot; title=&quot;Working tabs and list&quot; /&gt;&lt;/p&gt;

&lt;p&gt;It looks ok, so we can move on.&lt;/p&gt;

&lt;h2 id=&quot;scrolling-toolbar-and-floatingactionbutton&quot;&gt;Scrolling Toolbar and FloatingActionButton&lt;/h2&gt;

&lt;p&gt;Now it’s time to make our Toolbar scrollable. If you have seen two previous parts of this post you’re probably thinking that we will be creating another &lt;code&gt;OnScrollListener&lt;/code&gt; or something. But it’s not the case. Actually we need to add only one line of code to out layout file:
&lt;script src=&quot;https://gist.github.com//mzgreen/fb0a56c98ee777d9e27f.js&quot;&gt; &lt;/script&gt;&lt;/p&gt;

&lt;p&gt;And that’s it! The &lt;code&gt;app:layout_scrollFlags=&quot;scroll|enterAlways&quot;&lt;/code&gt; line will cause that our &lt;code&gt;Toolbar&lt;/code&gt; will scroll of the screen when user scrolls down the list and as soon as he starts scrolling up the &lt;code&gt;Toolbar&lt;/code&gt; will show up again. Clean and simple, that’s the power of &lt;code&gt;CoordinatorLayout&lt;/code&gt;!&lt;/p&gt;

&lt;p&gt;The app now looks like this:
&lt;img src=&quot;/images/3/scrollingtoolbar.gif&quot; alt=&quot;Scrolling toolbar&quot; title=&quot;Scrolling toolbar&quot; /&gt;&lt;/p&gt;

&lt;p&gt;As you can see if you scroll the &lt;code&gt;Toolbar&lt;/code&gt; off the screen and change tabs, it remains scrolled off. And even that on the second page we have only 5 items, so list itself cannot be scrolled, you can still show the &lt;code&gt;Toolbar&lt;/code&gt;. The &lt;code&gt;CoordinatorLayout&lt;/code&gt; makes it possible.&lt;/p&gt;

&lt;p&gt;So the last part is adding a FAB. In &lt;a href=&quot;http://mzgreen.github.io/2015/02/15/How-to-hideshow-Toolbar-when-list-is-scroling%28part1%29/&quot;&gt;part 1&lt;/a&gt; we had to create it manually. We were using an &lt;code&gt;ImageButton&lt;/code&gt; with a circular drawable as a background. From now on we have a &lt;code&gt;FloatingActionButton&lt;/code&gt; widget in the &lt;a href=&quot;http://android-developers.blogspot.com/2015/05/android-design-support-library.html&quot;&gt;Design Support Library&lt;/a&gt;! Let’s add it to our &lt;code&gt;activity_part_three.xml&lt;/code&gt; layout file:
&lt;script src=&quot;https://gist.github.com//mzgreen/7119c87149c6d7a2ccfc.js&quot;&gt; &lt;/script&gt;&lt;/p&gt;

&lt;p&gt;There are some known issues with FAB and other widgets from &lt;a href=&quot;http://android-developers.blogspot.com/2015/05/android-design-support-library.html&quot;&gt;Design Support Library&lt;/a&gt; but most of them are already fixed so all we have to do is to wait for the release of the updated version. More on using new FAB widget you can read on &lt;a href=&quot;http://antonioleiva.com/floating-action-button/&quot;&gt;Antonio blog&lt;/a&gt;. Our app now have a &lt;code&gt;FloatingActionButton&lt;/code&gt;:
&lt;img src=&quot;/images/3/screenwithfab.png&quot; alt=&quot;App with fab&quot; title=&quot;App with fab&quot; /&gt;&lt;/p&gt;

&lt;p&gt;But if we start scrolling, the FAB stays in place and only the &lt;code&gt;Toolbar&lt;/code&gt; is moving. To address this issue we will create a custom &lt;code&gt;Behavior&lt;/code&gt; for our FAB. Let’s look at the code:
&lt;script src=&quot;https://gist.github.com//mzgreen/7b77aa4ec95f81c3bed0.js&quot;&gt; &lt;/script&gt;&lt;/p&gt;

&lt;p&gt;It’s not a lot of code as you can see. Let’s look at the important parts. First we extend from &lt;code&gt;CoordinatorLayout.Behavior&lt;/code&gt; class. Note that we have to override constructor taking &lt;code&gt;Context&lt;/code&gt; and &lt;code&gt;AttributeSet&lt;/code&gt; as parameters in order to use this &lt;code&gt;Behavior&lt;/code&gt; in our layout (xml) files.  Next method that we need to override is &lt;code&gt;layoutDependsOn&lt;/code&gt;. This method tells &lt;code&gt;CoordinatorLayout&lt;/code&gt; that we want our FAB to depend on &lt;code&gt;AppBarLayout&lt;/code&gt;. So from now on we will be notified about changes of an &lt;code&gt;AppBarLayout&lt;/code&gt; size or position. To receive this notification we have to override another method which is &lt;code&gt;onDependentViewChanged&lt;/code&gt;. In this method we are calculating how much of the &lt;code&gt;AppBarLayout&lt;/code&gt; is scrolled off the screen and we are scrolling our FAB accordingly. So if a half of our &lt;code&gt;AppBarLayout&lt;/code&gt; is scrolled off then a half of FAB should also be scrolled off. That’s basically it. Calculating ratio and moving FAB. We return &lt;code&gt;true&lt;/code&gt; from this method because it changes position of child view (FAB in our case). What we need to do right now is to add this behavior to our FAB. Let’s do it then:
&lt;script src=&quot;https://gist.github.com//mzgreen/2002e3dd979e22f47cf1.js&quot;&gt; &lt;/script&gt;&lt;/p&gt;

&lt;p&gt;As you can see we are using fully qualified name of our &lt;code&gt;Behavior&lt;/code&gt; class.&lt;/p&gt;

&lt;p&gt;Let’s see how it works:
&lt;img src=&quot;/images/3/scrollingtoolbarandfab.gif&quot; alt=&quot;Working example gif&quot; title=&quot;Working example gif&quot; /&gt;&lt;/p&gt;

&lt;p&gt;Great, now the FAB moves with our &lt;code&gt;Toolbar&lt;/code&gt;!&lt;/p&gt;

&lt;p&gt;As you can see the new &lt;a href=&quot;http://android-developers.blogspot.com/2015/05/android-design-support-library.html&quot;&gt;Design Support Library&lt;/a&gt; is really powerful. Using it is pretty simple but it takes some time to get used to new stuff. I again encourage you to play with it and see what else you can do. &lt;code&gt;Behaviors&lt;/code&gt; are very flexible and there is a lot more methods available in there. As I have said there are still some rough edges. For example the &lt;code&gt;Toolbar&lt;/code&gt; is not snapping to the position, there is some issue with &lt;code&gt;Overscroll&lt;/code&gt; which shows when you fling up but not when you scroll up etc. Most of these issues has status &lt;code&gt;FutureRelease&lt;/code&gt; so we will get the fixes soon :)&lt;/p&gt;

&lt;h3 id=&quot;code&quot;&gt;Code&lt;/h3&gt;
&lt;p&gt;Source code of the full project described in this post is available on GitHub &lt;a href=&quot;https://github.com/mzgreen/HideOnScrollExample&quot;&gt;repo&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;- Michał Z.&lt;/em&gt;&lt;/p&gt;

</content>
 </entry>
 
 <entry>
   <title>How to hide/show Toolbar when list is scrolling (part 2)</title>
   <link href="http://mzgreen.github.io//2015/02/28/How-to-hideshow-Toolbar-when-list-is-scrolling(part2)/"/>
   <updated>2015-02-28T00:00:00+00:00</updated>
   <id>http://mzgreen.github.io/2015/02/28/How-to-hideshow-Toolbar-when-list-is-scrolling(part2)</id>
   <content type="html">&lt;p&gt;This post is outdated, you should jump to &lt;a href=&quot;https://mzgreen.github.io/2015/06/23/How-to-hideshow-Toolbar-when-list-is-scrolling%28part3%29/&quot;&gt;part 3&lt;/a&gt; or stay if you are bored :)&lt;/p&gt;

&lt;p&gt;This is a second (and the last) post of this series. If you haven’t read &lt;a href=&quot;http://mzgreen.github.io/2015/02/15/How-to-hideshow-Toolbar-when-list-is-scroling%28part1%29/&quot;&gt;part 1&lt;/a&gt; I recommend you to do it. In &lt;a href=&quot;http://mzgreen.github.io/2015/02/15/How-to-hideshow-Toolbar-when-list-is-scroling%28part1%29/&quot;&gt;previous part&lt;/a&gt; we’ve learned how to achieve an effect of hiding Toolbar like Google+. Today we will see how we can make it to behave like Google Play Store Toolbar.  Let’s begin!&lt;/p&gt;

&lt;p&gt;Before we start, I would like to say that I’ve refactored this &lt;a href=&quot;https://github.com/mzgreen/HideOnScrollExample&quot;&gt;project&lt;/a&gt; a little bit - it’s split in two &lt;code&gt;Activities&lt;/code&gt;: &lt;code&gt;PartOneActivity&lt;/code&gt; and &lt;code&gt;PartTwoActivity&lt;/code&gt; that are started from &lt;code&gt;MainActivity&lt;/code&gt;. Code is in the packages &lt;code&gt;partone&lt;/code&gt; and &lt;code&gt;parttwo&lt;/code&gt; so you can easily find classes that interest you.&lt;/p&gt;

&lt;p&gt;This is how our final effect will look compared to Play Store Toolbar:
&lt;img src=&quot;/images/2/goal.gif&quot; alt=&quot;Working example gif&quot; title=&quot;Working example gif&quot; /&gt; &lt;img src=&quot;/images/2/playstore.gif&quot; alt=&quot;Play Store gif&quot; title=&quot;Play Store gif&quot; /&gt;&lt;/p&gt;

&lt;h2 id=&quot;first-things-first&quot;&gt;First things first&lt;/h2&gt;
&lt;p&gt;I won’t show &lt;code&gt;build.gradle&lt;/code&gt; file because it’s the same as in &lt;a href=&quot;http://mzgreen.github.io/2015/02/15/How-to-hideshow-Toolbar-when-list-is-scroling%28part1%29/&quot;&gt;part 1&lt;/a&gt; so we will start from creating a layout for our &lt;code&gt;Activity&lt;/code&gt;:
&lt;script src=&quot;https://gist.github.com//mzgreen/54faa3e44ae3de8a682c.js&quot;&gt; &lt;/script&gt;&lt;/p&gt;

&lt;p&gt;It’s just a &lt;code&gt;RecyclerView&lt;/code&gt; and a &lt;code&gt;Toolbar&lt;/code&gt; (we will add &lt;code&gt;Tabs&lt;/code&gt; later). Notice that I’m using the second method (with adding padding to &lt;code&gt;RecyclerView&lt;/code&gt;) described in &lt;a href=&quot;http://mzgreen.github.io/2015/02/15/How-to-hideshow-Toolbar-when-list-is-scroling%28part1%29/&quot;&gt;previous post&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;I will omit a layout file of our list item because it’s exactly the same as &lt;a href=&quot;https://gist.github.com/mzgreen/b44afa284a0c3c4e0b9c#file-recycler_item-xml&quot;&gt;before&lt;/a&gt;. &lt;code&gt;RecyclerAdapter&lt;/code&gt; was also described so we can skip it (&lt;a href=&quot;https://gist.github.com/mzgreen/f0dc97062bb5f1c534b1#file-recycleradapter-java&quot;&gt;here&lt;/a&gt; is how it should look - simple adapter, without header).&lt;/p&gt;

&lt;p&gt;Let’s jump to our &lt;code&gt;PartTwoActivity&lt;/code&gt; code:
&lt;script src=&quot;https://gist.github.com//mzgreen/72ef88c35fae6e979f15.js&quot;&gt; &lt;/script&gt;&lt;/p&gt;

&lt;p&gt;It’s a basic initialization of &lt;code&gt;RecyclerView&lt;/code&gt; and &lt;code&gt;Toolbar&lt;/code&gt;, notice setting &lt;code&gt;OnScrollListener&lt;/code&gt; in line 27.&lt;/p&gt;

&lt;p&gt;The most interesting part is &lt;code&gt;HidingScrollListener&lt;/code&gt;, so let’s create it!
&lt;script src=&quot;https://gist.github.com//mzgreen/95f93c615456c1d72104.js&quot;&gt; &lt;/script&gt;&lt;/p&gt;

&lt;p&gt;If you’ve read &lt;a href=&quot;http://mzgreen.github.io/2015/02/15/How-to-hideshow-Toolbar-when-list-is-scroling%28part1%29/&quot;&gt;previous part&lt;/a&gt; it should look familiar (actually it’s even simpler right now). What do we have here?
There is only one important variable so far - &lt;code&gt;mToolbarOffset&lt;/code&gt; which holds a scrolled offset relative to &lt;code&gt;Toolbar&#39;s&lt;/code&gt; height. To put it simply, we want to track only values between 0 and &lt;code&gt;Toolbar&lt;/code&gt; height so thanks to this:
&lt;script src=&quot;https://gist.github.com//mzgreen/5610dfaaf9756b6d456a.js&quot;&gt; &lt;/script&gt;
it will be increased if we scroll up (but we don’t want it to be bigger than &lt;code&gt;Toolbar&#39;s&lt;/code&gt; height) and decreased if we scroll down (again we don’t want it to be lower than 0). You will see why do we restrict these values soon.
We are also clipping &lt;code&gt;mToolbarOffset&lt;/code&gt; because it’s possible that it will have some value out of our range for a small period of time (e.g. during fling) and it would cause flickering.
We’ve also defined  &lt;code&gt;onMoved()&lt;/code&gt; - an abstract method which we call during scroll.
It may surprise you but this is it for now!&lt;/p&gt;

&lt;p&gt;We have to get back to our &lt;code&gt;PartTwoActivity&lt;/code&gt; and implement &lt;code&gt;onMoved()&lt;/code&gt; method inside our scroll listener:
&lt;script src=&quot;https://gist.github.com//mzgreen/e108819d31a9e675452b.js&quot;&gt; &lt;/script&gt;&lt;/p&gt;

&lt;p&gt;Yup, that’s all. We can run our app and see what do we have:
&lt;img src=&quot;/images/2/nosnap.gif&quot; alt=&quot;No snap gif&quot; title=&quot;No snap gif&quot; /&gt;&lt;/p&gt;

&lt;p&gt;Pretty good, the &lt;code&gt;Toolbar&lt;/code&gt; is moving along with the list and getting back just as we expect it to. This is thanks to the restrictions that we put on the &lt;code&gt;mToolbarOffset&lt;/code&gt; variable. If we would omit checking if it’s bigger than 0 and lower than &lt;code&gt;mToolbarHeight&lt;/code&gt; then when we would scroll up our list, the &lt;code&gt;Toolbar&lt;/code&gt; would move along far away off the screen, so to show it back you would have to scroll the list down to 0. Right now it just scrolls up to &lt;code&gt;mToolbarHeight&lt;/code&gt; position and not more so it’s “sitting” right above the list all of the time and if we start scrolling down, we can see it immediately showing.&lt;/p&gt;

&lt;p&gt;It works pretty well, but this is not what we want. It feels weird that you can stop it in the middle of the scroll and the &lt;code&gt;Toolbar&lt;/code&gt; will stay half visible. Actually this is how it’s done in Google Play Games app which I consider as a bug.&lt;/p&gt;

&lt;h2 id=&quot;snapping-the-toolbar&quot;&gt;Snapping the Toolbar&lt;/h2&gt;
&lt;p&gt;I think that views should smoothly snap to the position like Logo/SearchBar in Chrome app or &lt;code&gt;Toolbar&lt;/code&gt; in Play Store app. I’m pretty sure that I saw it somewhere in Material guidelines/checklist or heard in one of Google I/O presentations.&lt;/p&gt;

&lt;p&gt;Let’s revisit out &lt;code&gt;HidingScrollListener&lt;/code&gt; code:
&lt;script src=&quot;https://gist.github.com//mzgreen/3e867d8db87aa23aa376.js&quot;&gt; &lt;/script&gt;&lt;/p&gt;

&lt;p&gt;It got a little bit more complicated but there is nothing scary in there. We’ve just overrided the second method of the &lt;code&gt;RecyclerView.OnScrollListener&lt;/code&gt; class which is &lt;code&gt;onScrollStateChanged()&lt;/code&gt;. This is what we’re doing in this method:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;We are checking if the list is in &lt;code&gt;RecyclerView.SCROLL_STATE_IDLE&lt;/code&gt; state so it’s not scrolling nor flinging (because if it is, we’are translating Y position of the &lt;code&gt;Toolbar&lt;/code&gt; manually - like before).&lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;If we lift up our finger and list has stopped (it’s in &lt;code&gt;RecyclerView.SCROLL_STATE_IDLE&lt;/code&gt; state) we have to check if it’s visible and if it is, then this means that we have to hide it if &lt;code&gt;mToolbarOffset&lt;/code&gt; is bigger than &lt;code&gt;HIDE_THRESHOLD&lt;/code&gt; or we have to show it again if &lt;code&gt;mToolbarOffset&lt;/code&gt; is lower than &lt;code&gt;SHOW_THRESHOLD&lt;/code&gt;:
&lt;script src=&quot;https://gist.github.com//mzgreen/a4f4fb043bc902e386c5.js&quot;&gt; &lt;/script&gt;&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;And if it’s not visible then we have to do the opposite - if &lt;code&gt;mToolbarOffset&lt;/code&gt; (which now is calculated from top position so it’s &lt;code&gt;mToolbarHeight - mToolbarOffset&lt;/code&gt;) is bigger than &lt;code&gt;SHOW_THRESHOLD&lt;/code&gt; then we are showing it and if it’s lower than &lt;code&gt;HIDE_THRESHOLD&lt;/code&gt; then we are hiding it again:
&lt;script src=&quot;https://gist.github.com//mzgreen/fa7bb444c05315258dd3.js&quot;&gt; &lt;/script&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;code&gt;onScrolled()&lt;/code&gt; stays the same as it was, and we don’t have to change anything else here. The last thing that we need to do is to implement our two new abstract methods in &lt;code&gt;PartTwoActivity&lt;/code&gt; class:
&lt;script src=&quot;https://gist.github.com//mzgreen/29359ccda3b5e1c68093.js&quot;&gt; &lt;/script&gt;&lt;/p&gt;

&lt;p&gt;It’s time to build our project and see the effect:
&lt;img src=&quot;/images/2/snapnotabs.gif&quot; alt=&quot;Snap no tabs gif&quot; title=&quot;Snap no tabs gif&quot; /&gt;&lt;/p&gt;

&lt;p&gt;Looks like it’s working! :)&lt;/p&gt;

&lt;p&gt;You may think that adding the tabs will complicate the code, so let me show you that it’s not the case.&lt;/p&gt;

&lt;h2 id=&quot;adding-tabs&quot;&gt;Adding Tabs&lt;/h2&gt;

&lt;p&gt;We need to modify our &lt;code&gt;Activity&#39;s&lt;/code&gt; layout:
&lt;script src=&quot;https://gist.github.com//mzgreen/0f8111db8b780efeb372.js&quot;&gt; &lt;/script&gt;&lt;/p&gt;

&lt;p&gt;and &lt;code&gt;tabs.xml&lt;/code&gt;:
&lt;script src=&quot;https://gist.github.com//mzgreen/d77ce91f21e9ceace291.js&quot;&gt; &lt;/script&gt;&lt;/p&gt;

&lt;p&gt;As you can see I’m not adding real &lt;code&gt;Tabs&lt;/code&gt;, just a layout that mimics their look. It won’t change anything in the implementation. You can put any view here. There are some implementations of the &lt;code&gt;Tabs&lt;/code&gt; for Material Design available on github or you can create them yourself :)&lt;/p&gt;

&lt;p&gt;Adding the &lt;code&gt;Tabs&lt;/code&gt; means that they will cover our list a little, so we need to increase the padding. To make it  flexible we won’t be setting this in xml (notice removed padding from &lt;code&gt;RecyclerView&lt;/code&gt; in &lt;code&gt;part_two_activity.xml&lt;/code&gt;), because &lt;code&gt;Toolbar&lt;/code&gt; can have different height depending on orientation or device (e.g on tablets), so we would have to create a bunch of xml to cover all the cases. Instead we will set the padding in code:
&lt;script src=&quot;https://gist.github.com//mzgreen/d3197b0faeb0f8103b71.js&quot;&gt; &lt;/script&gt;
It’s pretty simple, we’re setting the padding to be the sum of &lt;code&gt;Toolbar&#39;s&lt;/code&gt; height and &lt;code&gt;Tabs&#39;&lt;/code&gt; height.
If we run this now we will see something like this:
&lt;img src=&quot;/images/2/withtabs.png&quot; alt=&quot;Screen with tabs&quot; title=&quot;Screen with tabs&quot; /&gt;&lt;/p&gt;

&lt;p&gt;It’s all good, our first list item is perfectly visible, so we can move along. Actually we won’t change anything in our &lt;code&gt;HidingScrollListener&lt;/code&gt; class. The only change that needs to be done is in &lt;code&gt;PartTwoActivity&lt;/code&gt;:
&lt;script src=&quot;https://gist.github.com//mzgreen/28e057f34b7e51bb1592.js&quot;&gt; &lt;/script&gt;&lt;/p&gt;

&lt;p&gt;Can you see what has changed? We are getting &lt;code&gt;mToolbarContainer&lt;/code&gt; reference which is a &lt;code&gt;LinearLayout&lt;/code&gt; instead of &lt;code&gt;Toolbar&lt;/code&gt;, and in &lt;code&gt;onMove()&lt;/code&gt;, &lt;code&gt;onHide()&lt;/code&gt; and &lt;code&gt;onShow()&lt;/code&gt; methods we are translating and animating this view instead of &lt;code&gt;Toolbar&lt;/code&gt;. This will move a whole container that contains the &lt;code&gt;Toolbar&lt;/code&gt; and &lt;code&gt;Tabs&lt;/code&gt; and this is exactly what we need to do.&lt;/p&gt;

&lt;p&gt;If we run it we can see that it seems to be working as expected, but if you look closely you will see that there is a little bug in there. Sometimes there is a white line visible between &lt;code&gt;Tabs&lt;/code&gt; and &lt;code&gt;Toolbar&lt;/code&gt; for the fraction of a second. It’s probably because they are not perfectly synchronized when they are animating. Fortunately it’s not something that we couldn’t fix:)&lt;/p&gt;

&lt;p&gt;The fix is very simple, just put the background of the &lt;code&gt;Toolbar&lt;/code&gt; and &lt;code&gt;Tabs&lt;/code&gt; to their parent layout:
&lt;script src=&quot;https://gist.github.com//mzgreen/ce503aeba273c322abbb.js&quot;&gt; &lt;/script&gt;&lt;/p&gt;

&lt;p&gt;Now even if views are not perfectly synchronized during animation, it won’t be visible.
There is one more bug, the same that we had in &lt;a href=&quot;http://mzgreen.github.io/2015/02/15/How-to-hideshow-Toolbar-when-list-is-scroling%28part1%29/&quot;&gt;part 1&lt;/a&gt;. If we are at the top of the list, we can scroll a little bit up and if the &lt;code&gt;HIDE_THRESHOLD&lt;/code&gt; small enough, the &lt;code&gt;Toolbar&lt;/code&gt; will hide and there will be an empty space(padding) visible above the list. Again - fix is really simple:
&lt;script src=&quot;https://gist.github.com//mzgreen/09ef2f24440fbef5f17a.js&quot;&gt; &lt;/script&gt;&lt;/p&gt;

&lt;p&gt;We’ve just added one more variable that holds total scroll offset of the list, and when we are about to check if we should show or hide the &lt;code&gt;Toolbar&lt;/code&gt;, we first check if we scrolled more than &lt;code&gt;Toolbar&#39;s&lt;/code&gt; height (if not, we show the &lt;code&gt;Toolbar&lt;/code&gt; again).&lt;/p&gt;

&lt;p&gt;This is it, let’s run our app!
&lt;img src=&quot;/images/2/goal.gif&quot; alt=&quot;Working example gif&quot; title=&quot;Working example gif&quot; /&gt;&lt;/p&gt;

&lt;p&gt;It’s working very well now :) And it even works with other LayoutManagers without changing anything else:
&lt;script src=&quot;https://gist.github.com//mzgreen/892c394d6db47eb3708b.js&quot;&gt; &lt;/script&gt;&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/images/2/grid.png&quot; alt=&quot;Grid layout&quot; title=&quot;Grid layout&quot; /&gt;&lt;/p&gt;

&lt;p&gt;There was a question in the comment about saving scroll state, and it truly is a problem a little bit. If our item’s text would be long enough to be 2 lines in portrait mode and 1 line in landscape mode then our items heights will be different in portrait and landscape. So if we would scroll to position let’s say 100 in portrait and rotate the device with saving &lt;code&gt;mTotalScrolledDistance&lt;/code&gt; and after rotation we would scroll up the list to the top, then &lt;code&gt;mTotalScrolledDistance&lt;/code&gt; could be different than 0. There is no simple fix for that but for our usecase it doesn’t matter. And if you really want to do something about it, I would reset &lt;code&gt;mTotalScrolledDistance&lt;/code&gt; to 0 and show the &lt;code&gt;Toolbar&lt;/code&gt; after rotation.&lt;/p&gt;

&lt;p&gt;So this is the end of this series of posts. I’m glad that some of you have learned something from the previous part. Thanks for the kind words :) I will continue writing this blog, but I don’t know what will be in the next posts yet:)&lt;/p&gt;

&lt;p&gt;I also want to say that I think that methods described in this and &lt;a href=&quot;http://mzgreen.github.io/2015/02/15/How-to-hideshow-Toolbar-when-list-is-scroling%28part1%29/&quot;&gt;previous&lt;/a&gt; post work pretty good but I didn’t test them well enough, so I’m not sure if they are ready to use in production code (see for example the saving state problem described above). The goal of this series was to show that you can achieve effects that looks difficult using only simple methods and standard APIs. I’ve also found out that this method can be used for many other things (e.g. creating sticking tabs with parallax background - like in Google+ profile screen).
Happy coding!&lt;/p&gt;

&lt;h3 id=&quot;code&quot;&gt;Code&lt;/h3&gt;
&lt;p&gt;Source code of the full project described in this post is available on GitHub &lt;a href=&quot;https://github.com/mzgreen/HideOnScrollExample&quot;&gt;repo&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;- Michał Z.&lt;/em&gt;&lt;/p&gt;

</content>
 </entry>
 
 <entry>
   <title>How to hide/show Toolbar when list is scroling (part 1)</title>
   <link href="http://mzgreen.github.io//2015/02/15/How-to-hideshow-Toolbar-when-list-is-scroling(part1)/"/>
   <updated>2015-02-15T00:00:00+00:00</updated>
   <id>http://mzgreen.github.io/2015/02/15/How-to-hideshow-Toolbar-when-list-is-scroling(part1)</id>
   <content type="html">&lt;p&gt;This post is outdated, you should jump to &lt;a href=&quot;https://mzgreen.github.io/2015/06/23/How-to-hideshow-Toolbar-when-list-is-scrolling%28part3%29/&quot;&gt;part 3&lt;/a&gt; or stay if you are bored :)&lt;/p&gt;

&lt;p&gt;In this post we will see how to achieve an effect that you can observe in Google+ app - hiding Toolbar and FAB (and any other views) when list is scrolling down and showing it again when it’s scrolling up. This behavior is mentioned in &lt;a href=&quot;http://android-developers.blogspot.com/2014/10/material-design-on-android-checklist.html&quot;&gt;Material Design Checklist&lt;/a&gt;.&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;“Where appropriate, upon scrolling down, the app bar can scroll off the screen, leaving more vertical space for content. Upon scrolling back up, the app bar should be shown again.”&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;This is how our goal should look:
&lt;img src=&quot;/images/1/demo_gif.gif&quot; alt=&quot;Working example screenshot&quot; title=&quot;Working example screenshot&quot; /&gt;&lt;/p&gt;

&lt;p&gt;We will be using &lt;code&gt;RecyclerView&lt;/code&gt; for our list but it’s possible to implement it in any other scrolling container (with a little more work in some cases i.e. &lt;code&gt;ListView&lt;/code&gt;).
There are two ways that come to my mind on how to achieve this:&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;With adding a padding to the list. &lt;/li&gt;
  &lt;li&gt;With adding a header to the list.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;I decided to implement only the second one because I saw multiple questions on how to add a header to &lt;code&gt;RecyclerView&lt;/code&gt; and this is a good opportunity to cover this but I will also briefly descripe the first one.&lt;/p&gt;

&lt;h2 id=&quot;lets-get-started&quot;&gt;Let’s get started!&lt;/h2&gt;

&lt;p&gt;We will begin from creating our project and adding necesarry libraries:
&lt;script src=&quot;https://gist.github.com/mzgreen/74aa1238045fea357142.js&quot;&gt; &lt;/script&gt;&lt;/p&gt;

&lt;p&gt;Now we should define &lt;code&gt;styles.xml&lt;/code&gt; so that our app will use Material Theme but without &lt;code&gt;ActionBar&lt;/code&gt; (we will be using &lt;code&gt;Toolbar&lt;/code&gt;):
&lt;script src=&quot;https://gist.github.com/mzgreen/369937dcd0ad6e859109.js&quot;&gt; &lt;/script&gt;&lt;/p&gt;

&lt;p&gt;The next thing is to create our &lt;code&gt;Activity&lt;/code&gt; layout:
&lt;script src=&quot;https://gist.github.com/mzgreen/8374572ce6057415113f.js&quot;&gt; &lt;/script&gt;&lt;/p&gt;

&lt;p&gt;It’s a simple layout with &lt;code&gt;RecyclerView&lt;/code&gt;, &lt;code&gt;Toolbar&lt;/code&gt; and &lt;code&gt;ImageButton&lt;/code&gt; which will be our FAB. We need to put them in a &lt;code&gt;FrameLayout&lt;/code&gt; because &lt;code&gt;Toolbar&lt;/code&gt; needs to be overlayed on &lt;code&gt;RecyclerView&lt;/code&gt;. If we don’t do this, there will be an empty space visible above the list when we hide the &lt;code&gt;Toolbar&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Let’s jump into the code of our &lt;code&gt;MainActivity&lt;/code&gt;:
&lt;script src=&quot;https://gist.github.com/mzgreen/40201cd9aa1054aebfb4.js&quot;&gt; &lt;/script&gt;&lt;/p&gt;

&lt;p&gt;As you can see it’s a relatively small class. It only implements onCreate which does the following things:&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;Initializing &lt;code&gt;Toolbar&lt;/code&gt;&lt;/li&gt;
  &lt;li&gt;Getting a reference to our FAB&lt;/li&gt;
  &lt;li&gt;Initializing &lt;code&gt;RecyclerView&lt;/code&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Now we will create an adapter for our &lt;code&gt;RecyclerView&lt;/code&gt;. But first, we have to add a layout for our list items:
&lt;script src=&quot;https://gist.github.com/mzgreen/b44afa284a0c3c4e0b9c.js&quot;&gt; &lt;/script&gt;&lt;/p&gt;

&lt;p&gt;And corresponding &lt;code&gt;ViewHolder&lt;/code&gt;:
&lt;script src=&quot;https://gist.github.com/mzgreen/02359283cb0827e62ed0.js&quot;&gt; &lt;/script&gt;&lt;/p&gt;

&lt;p&gt;Our list will be showing cards with only text so this is it - easy!&lt;/p&gt;

&lt;p&gt;Now we can jump to the &lt;code&gt;RecyclerAdapter&lt;/code&gt; code:
&lt;script src=&quot;https://gist.github.com/mzgreen/f0dc97062bb5f1c534b1.js&quot;&gt; &lt;/script&gt;&lt;/p&gt;

&lt;p&gt;It’s a basic &lt;code&gt;RecyclerView.Adapter&lt;/code&gt; implementation. There is nothing special about it. If you want to find about more about &lt;code&gt;RecyclerView&lt;/code&gt;, I recommend you reading Mark Allison’s great &lt;a href=&quot;https://blog.stylingandroid.com/material-part-4/&quot;&gt;series of posts&lt;/a&gt; &lt;/p&gt;

&lt;p&gt;We got all the pieces in place so let’s run it!
&lt;img src=&quot;/images/1/clipped.png&quot; alt=&quot;Clipped screenshot&quot; title=&quot;Clipped screenshot&quot; /&gt;&lt;/p&gt;

&lt;p&gt;Oh wait.. what is this? The &lt;code&gt;Toolbar&lt;/code&gt; hides our list items and as you have probably noticed it’s because we are using &lt;code&gt;FrameLayout&lt;/code&gt; in our &lt;code&gt;activity_main.xml&lt;/code&gt;. This is the moment when we have two options that I mentioned at te beginning.
First option will be to add a paddingTop to our &lt;code&gt;RecyclerVie&lt;/code&gt;w and set it to &lt;code&gt;Toolbar&lt;/code&gt; height. But we have to be careful because &lt;code&gt;RecyclerView&lt;/code&gt; will clip it’s chilren to padding by default so we have to turn it off. Our layout would look like this:
&lt;script src=&quot;https://gist.github.com/mzgreen/870bc2f012dc14626e8d.js&quot;&gt; &lt;/script&gt;&lt;/p&gt;

&lt;p&gt;And it would do the thing. But as I said, I wanted to show you another way - maybe a little more complicated which involves adding a header to the list.&lt;/p&gt;

&lt;h2 id=&quot;adding-a-header-to-the-recyclerview&quot;&gt;Adding a header to the &lt;code&gt;RecyclerView&lt;/code&gt;:&lt;/h2&gt;

&lt;p&gt;First we need to modify our &lt;code&gt;Adapter&lt;/code&gt; a little:
&lt;script src=&quot;https://gist.github.com/mzgreen/e95e138cfd95d955949c.js&quot;&gt; &lt;/script&gt;&lt;/p&gt;

&lt;p&gt;Here is how it works:&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;We need to define types of items that the &lt;code&gt;Recycler&lt;/code&gt; will display. &lt;code&gt;RecyclerView&lt;/code&gt; is a very flexible component. Item types are used when you want to have different layout for some of your list items. And this is exactly what we want to do - our first item will be a header view, so it will be different from the rest of items (lines 3-4).&lt;/li&gt;
  &lt;li&gt;We need to tell the &lt;code&gt;Recycler&lt;/code&gt; which type item it wants to display is (lines 49-54).&lt;/li&gt;
  &lt;li&gt;We need to modify &lt;code&gt;onCreateViewHolder&lt;/code&gt; and &lt;code&gt;onBindViewHolder()&lt;/code&gt; methods to return and bind a normal item if it’s type is &lt;code&gt;TYPE_ITEM&lt;/code&gt; and a header item if it’s type is &lt;code&gt;TYPE_HEADER&lt;/code&gt; (lines 14-34).&lt;/li&gt;
  &lt;li&gt;We need to modify &lt;code&gt;getItemCount()&lt;/code&gt; - we return a number of items in our dataset +1 because we have also a header (lines 43-45).&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Now let’s create a layout and &lt;code&gt;ViewHolder&lt;/code&gt; for the header view.&lt;/p&gt;

&lt;script src=&quot;https://gist.github.com/mzgreen/0b81a1b62a681e0fc208.js&quot;&gt; &lt;/script&gt;

&lt;p&gt;The layout is very simple. Important thing to notice is that it’s height needs to be equal to our &lt;code&gt;Toolbar&lt;/code&gt; height. And it’s &lt;code&gt;ViewHolder&lt;/code&gt; is also pretty straightforward:
&lt;script src=&quot;https://gist.github.com/mzgreen/670be3ca26409c52a609.js&quot;&gt; &lt;/script&gt;&lt;/p&gt;

&lt;p&gt;Ok, it’s done so we can try it out!
&lt;img src=&quot;/images/1/clipping_fixed.png&quot; alt=&quot;Fixed clipping screenshot&quot; title=&quot;Fixed clipping screenshot&quot; /&gt;&lt;/p&gt;

&lt;p&gt;Much better, right?
So to sum up, we have added a header to our &lt;code&gt;RecyclerView&lt;/code&gt; that has the same height as &lt;code&gt;Toolbar&lt;/code&gt; and now our &lt;code&gt;Toolbar&lt;/code&gt; hides header view (which is an empty view) and all of our list items are perfectly visible.
And finally we can implement showing/hiding views when list is scrolling.&lt;/p&gt;

&lt;h2 id=&quot;showinghiding-views-when-list-is-scrolling&quot;&gt;Showing/hiding views when list is scrolling.&lt;/h2&gt;

&lt;p&gt;To achieve this we will create only one more class - &lt;code&gt;OnScrollListener&lt;/code&gt; for &lt;code&gt;RecyclerView&lt;/code&gt;.
&lt;script src=&quot;https://gist.github.com/mzgreen/c48f1ae85d66a046d012.js&quot;&gt; &lt;/script&gt;&lt;/p&gt;

&lt;p&gt;As you can see there is only one method where all the magic happens - &lt;code&gt;onScrolled()&lt;/code&gt; method.
It’s parameters - dx, dy are the amounts of horizontal and vertical scrolls. Actually they are deltas, so it’s the amount between two events, not total scroll amount.&lt;/p&gt;

&lt;p&gt;Basically an algorithm works like this:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;
    &lt;p&gt;We are calculating total scroll amount (sum of deltas) but only if views are hidden and we are scrolling up or if views are visible and we are scrolling down because these are the cases that we care about.
&lt;script src=&quot;https://gist.github.com/mzgreen/0faae3937a0b83086899.js&quot;&gt; &lt;/script&gt;&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;Now if this total scroll amount exceeds some threshold (that you can adjust - the bigger it is, the more you have to scroll to show/hide views) we are showing/hiding views depending on the direction (dy&amp;gt;0 means that we are scrolling down, dy&amp;lt;0 means that we are scrolling up).
&lt;script src=&quot;https://gist.github.com/mzgreen/df58b5f74691ab030211.js&quot;&gt; &lt;/script&gt;&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;We aren’t actually showing/hiding views in our scroll listener class, instead we make it abstract and call show()/hide() methods, so the caller can implement them.&lt;/p&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Now we need to add this listener to our &lt;code&gt;RecyclerView&lt;/code&gt;:
&lt;script src=&quot;https://gist.github.com/mzgreen/58fb67f620d95bc45d87.js&quot;&gt; &lt;/script&gt;&lt;/p&gt;

&lt;p&gt;And here are the methods where we animate our views:
&lt;script src=&quot;https://gist.github.com/mzgreen/dcc0b173f22fcec971ea.js&quot;&gt; &lt;/script&gt;&lt;/p&gt;

&lt;p&gt;We have to take margins into account when we are hiding views, otherwise fab would’t fully hide.&lt;/p&gt;

&lt;p&gt;It’s time to test our app!
&lt;img src=&quot;/images/1/broken_gif.gif&quot; alt=&quot;Broken scrolling screenshot&quot; title=&quot;Broken scrolling screenshot&quot; /&gt;&lt;/p&gt;

&lt;p&gt;It looks almost good. Almost because there is a little bug - if you are at the top of the list and threshold is small, you can hide the &lt;code&gt;Toolbar&lt;/code&gt; and have empty space at the top of the list visible. Fortunately there is an easy fix for this. All we need to do is to detect if the first item of the list is visible and trigger our show/hide logic only if it’s not.
&lt;script src=&quot;https://gist.github.com/mzgreen/b386f4fb5d3cd82b0de0.js&quot;&gt; &lt;/script&gt;&lt;/p&gt;

&lt;p&gt;After this change if the first item is visible and views are hidder, we are showing them, otherwise it works as before. Let’s run our project again and see if it helped.
&lt;img src=&quot;/images/1/demo_gif.gif&quot; alt=&quot;Working example screenshot&quot; title=&quot;Working example screenshot&quot; /&gt;&lt;/p&gt;

&lt;p&gt;Yup! It seems like everything is working like a charm now :)&lt;/p&gt;

&lt;p&gt;It was the first blog post in my life so forgive me if it was boring or if I have made some mistakes. I will improve in the future.&lt;/p&gt;

&lt;p&gt;And if you don’t want to use the method with adding a header, you can still use the second one with adding padding to the &lt;code&gt;RecyclerView&lt;/code&gt;. Just add the padding and use &lt;code&gt;HidingScrollListener&lt;/code&gt; that we’ve just created and it will work :)&lt;/p&gt;

&lt;p&gt;In the next part I will show you how to make it to behave like scrolling in Google Play Store app.&lt;/p&gt;

&lt;p&gt;If you have any questions feel free to ask them in the comments below.&lt;/p&gt;

&lt;h3 id=&quot;code&quot;&gt;Code&lt;/h3&gt;
&lt;p&gt;Source code of the full project described in this post is available on GitHub &lt;a href=&quot;https://github.com/mzgreen/HideOnScrollExample&quot;&gt;repo&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Thanks to &lt;a href=&quot;https://twitter.com/froger_mcs&quot;&gt;Mirek Stanek&lt;/a&gt; for proof reading this post. &lt;/p&gt;

&lt;p&gt;&lt;em&gt;- Michał Z.&lt;/em&gt;&lt;/p&gt;

</content>
 </entry>
 

</feed>
