Last active
July 9, 2019 11:58
-
-
Save Gnzlt/c36c80ca841f396b4496b6efd4d266ef to your computer and use it in GitHub Desktop.
Android CoordinatorLayout Scroll Aware ExtendedFloatingActionButton behavior
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
| package com.example | |
| import android.content.Context | |
| import android.util.AttributeSet | |
| import android.view.View | |
| import androidx.coordinatorlayout.widget.CoordinatorLayout | |
| import androidx.core.view.ViewCompat | |
| import androidx.core.view.isVisible | |
| import com.google.android.material.floatingactionbutton.ExtendedFloatingActionButton | |
| class ScrollAwareExtendedFabBehavior : CoordinatorLayout.Behavior<ExtendedFloatingActionButton> { | |
| constructor() : super() | |
| constructor(context: Context, attributeSet: AttributeSet) : super(context, attributeSet) | |
| override fun onStartNestedScroll( | |
| coordinatorLayout: CoordinatorLayout, | |
| child: ExtendedFloatingActionButton, | |
| directTargetChild: View, | |
| target: View, | |
| axes: Int, | |
| type: Int | |
| ): Boolean = | |
| axes == ViewCompat.SCROLL_AXIS_VERTICAL || | |
| super.onStartNestedScroll( | |
| coordinatorLayout, | |
| child, | |
| directTargetChild, | |
| target, | |
| axes, | |
| type | |
| ) | |
| override fun onNestedScroll( | |
| coordinatorLayout: CoordinatorLayout, | |
| child: ExtendedFloatingActionButton, | |
| target: View, | |
| dxConsumed: Int, | |
| dyConsumed: Int, | |
| dxUnconsumed: Int, | |
| dyUnconsumed: Int, | |
| type: Int, | |
| consumed: IntArray | |
| ) { | |
| super.onNestedScroll( | |
| coordinatorLayout, | |
| child, | |
| target, | |
| dxConsumed, | |
| dyConsumed, | |
| dxUnconsumed, | |
| dyUnconsumed, | |
| type, | |
| consumed | |
| ) | |
| if (dyConsumed > 0 && child.isVisible) { | |
| // User scrolled down and the FAB is currently visible -> hide the FAB | |
| child.hide(object : ExtendedFloatingActionButton.OnChangedListener() { | |
| override fun onHidden(extendedFab: ExtendedFloatingActionButton?) { | |
| super.onHidden(extendedFab) | |
| extendedFab?.visibility = View.INVISIBLE | |
| } | |
| }) | |
| } else if (dyConsumed < 0 && !child.isVisible) { | |
| // User scrolled up and the FAB is currently not visible -> show the FAB | |
| child.show() | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment