Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save mostafa-hz/6e4a79a7d200aad945b603ddf43fd29a to your computer and use it in GitHub Desktop.
Save mostafa-hz/6e4a79a7d200aad945b603ddf43fd29a to your computer and use it in GitHub Desktop.
A Simple Coordinator Layout Behavior To Handle Shrinking And Extending ExtendedFloatingActionButton
package com.mosius
import android.content.Context
import android.util.AttributeSet
import android.view.View
import androidx.coordinatorlayout.widget.CoordinatorLayout
import androidx.core.view.ViewCompat
import com.google.android.material.floatingactionbutton.ExtendedFloatingActionButton
class ShrinkExtendedFloatingActionButtonOnScrollBehavior : CoordinatorLayout.Behavior<ExtendedFloatingActionButton> {
constructor() : super()
constructor(context: Context?, attrs: AttributeSet?) : super(context, attrs)
override fun onStartNestedScroll(
coordinatorLayout: CoordinatorLayout,
child: ExtendedFloatingActionButton,
directTargetChild: View,
target: View,
axes: Int,
type: Int
) = axes == ViewCompat.SCROLL_AXIS_VERTICAL
override fun onNestedScroll(
coordinatorLayout: CoordinatorLayout,
child: ExtendedFloatingActionButton,
target: View,
dxConsumed: Int,
dyConsumed: Int,
dxUnconsumed: Int,
dyUnconsumed: Int,
type: Int,
consumed: IntArray
) {
if (dyConsumed > 0) {
child.shrink()
} else if (dyConsumed < 0) {
child.extend()
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment