I'm trying to figure out how the expand/collapse animation of the toolbar is done. If you have a look at the Telegram app settings, you will see that there is a listview and the toolbar. When you scroll down, the toolbar collapse, and when you scroll up it expands. There is also the animation of the profile pic and the FAB. Does anyone have any clue on that? Do you think they built all the animations on top of it? Maybe I'm missing something from the new APIs or the support library.
I noticed the same behaviour on the Google calendar app, when you open the Spinner (I don't think it's a spinner, but it looks like): The toolbar expands and when you scroll up, it collapse.
Just to clearify: I don't need the QuickReturn method. I know that probably Telegram app is using something similar. The exact method that I need is the Google Calendar app effect. I've tried with
android:animateLayoutChanges="true"
and the expand method works pretty well. But obviously, If I scroll up the ListView, the toolbar doesn't collapse.
I've also thought about adding a GestureListener
but I want to know if there are any APIs or simpler methods of achieving this.
If there are none, I think I will go with the GestureListener
. Hopefully to have a smooth effect of the Animation.
Thanks!
Edit :
Since the release of the Android Design support library, there's an easier solution. Check joaquin's answer
--
Here's how I did it, there probably are many other solutions but this one worked for me.
First of all, you have to use a
Toolbar
with a transparent background. The expanding & collapsingToolbar
is actually a fake one that's under the transparentToolbar
. (you can see on the first screenshot below - the one with the margins - that this is also how they did it in Telegram).We only keep the actual
Toolbar
for theNavigationIcon
and the overflowMenuItem
.Everything that's in the red rectangle on the second screenshot (ie the fake
Toolbar
and theFloatingActionButton
) is actually a header that you add to the settingsListView
(orScrollView
).So you have to create a layout for this header in a separate file that could look like this :
(Note that you can use negative margins/padding for the fab to be straddling on 2
Views
)Now comes the interesting part. In order to animate the expansion of our fake
Toolbar
, we implement theListView
onScrollListener
.Note that there are some parts of this code I didn't test, so feel free to highlight mistakes. But overall, I'm know that this solution works, even though I'm sure it can be improved.
EDIT 2:
There were some mistakes in the code above (that I didn't test until today...), so I changed a few lines to make it work :
I changed the Y translation value applied to the header View from :
to :
Previous expression wasn't working at all, I'm sorry about that...
The ratio calculation also changed, so that it now evolves from the bottom the toolbar (instead of the top of the screen) to the full expanded header.
Also check out
CollapsingTitleLayout
written by Chris Banes in Android team: https://plus.google.com/+ChrisBanes/posts/J9Fwbc15BHNCode: https://gist.github.com/chrisbanes/91ac8a20acfbdc410a68
This is my implementation:
collapsedHeaderHeight
andexpandedHeaderHeight
are defined somewhere else, with the functiongetAnimationProgress
I can get the Expand/Collapse progress, base on this value I do my animation and show/hide the real header.Use design support library http://android-developers.blogspot.in/2015/05/android-design-support-library.html
include this in build.gradle
for recycler view include this also
Your activity should extend AppCompatActivity
Your app theme should be like this