Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save TylerMcCraw/bb5ad9a5ddc662f8f990b9ce8f084306 to your computer and use it in GitHub Desktop.
Save TylerMcCraw/bb5ad9a5ddc662f8f990b9ce8f084306 to your computer and use it in GitHub Desktop.
ScrollingViewAboveBottomNavigationBehavior
set this on your scrolling view for which you wish to have the behavior applied:
app:layout_behavior="com.myapp.mypackage.ScrollingViewAboveBottomNavigationBehavior"
import android.content.Context
import android.util.AttributeSet
import android.view.View
import androidx.coordinatorlayout.widget.CoordinatorLayout
import com.google.android.material.appbar.AppBarLayout
import com.google.android.material.bottomnavigation.BottomNavigationView
/**
* Extension of standard [AppBarLayout.ScrollingViewBehavior] which
* ensures the scrollview bottom aligns with the top of the view below it,
* such as a BottomNavigationView
*/
class ScrollingViewAboveBottomNavigationBehavior @JvmOverloads constructor(context: Context, attrs: AttributeSet? = null) :
AppBarLayout.ScrollingViewBehavior(context, attrs) {
private var marginBottom = 0
override fun layoutDependsOn(parent: CoordinatorLayout, child: View, dependency: View): Boolean =
super.layoutDependsOn(parent, child, dependency) || dependency is BottomNavigationView
override fun onDependentViewChanged(parent: CoordinatorLayout, child: View, dependency: View): Boolean {
return if (dependency is BottomNavigationView && dependency.getMeasuredHeight() != marginBottom) {
marginBottom = dependency.getMeasuredHeight()
child.layoutParams = (child.layoutParams as CoordinatorLayout.LayoutParams).apply {
bottomMargin = marginBottom
}
true
} else super.onDependentViewChanged(parent, child, dependency)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment