Created
September 29, 2017 11:27
-
-
Save MarcosCobena/ed36467fbe2aa44847ef94d42621bfab to your computer and use it in GitHub Desktop.
Custom Android's OnScrollListener to make FAB animate along with RecyclerView's scrolls
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| using FloatingActionMenu = Clans.Fab.FloatingActionMenu; | |
| public class AnimateFabAccordingOnScrollListener : Android.Support.V7.Widget.RecyclerView.OnScrollListener | |
| { | |
| const int AnimationDuration = 150; | |
| readonly FloatingActionMenu _fab; | |
| int _lastY; | |
| bool _fabTranslated; | |
| readonly int _bottomBarHeight; | |
| public AnimateFabAccordingOnScrollListener(FloatingActionMenu fab) : base() | |
| { | |
| _fab = fab; | |
| _bottomBarHeight = _fab.Resources.GetDimensionPixelSize(Resource.Dimension.bottombar_height); | |
| } | |
| public override void OnScrolled(Android.Support.V7.Widget.RecyclerView recyclerView, int dx, int dy) | |
| { | |
| base.OnScrolled(recyclerView, dx, dy); | |
| if (dy > 0 && !_fabTranslated) // Scrolling down for 1st time... | |
| { | |
| _fab.Animate() | |
| .TranslationY(_bottomBarHeight); | |
| _fabTranslated = true; | |
| } | |
| else if (dy < 0 && _fabTranslated) // Scrolling up for 1st time... | |
| { | |
| _fabTranslated = false; | |
| _fab.Animate() | |
| .TranslationY(0); | |
| } | |
| _lastY = dy; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment