Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BottomNavigationBar behaviour #15

Open
2 tasks
JoaquimLey opened this issue Mar 26, 2018 · 1 comment
Open
2 tasks

BottomNavigationBar behaviour #15

JoaquimLey opened this issue Mar 26, 2018 · 1 comment

Comments

@JoaquimLey
Copy link
Owner

JoaquimLey commented Mar 26, 2018

Makes sense to add a scrolling behavior to the BottomNavigationBar for better visibility

Here's the link to the article: https://android.jlelse.eu/scroll-your-bottom-navigation-view-away-with-10-lines-of-code-346f1ed40e9e?__s=ep727fgnkqv7dsftb1ut

Tasks ⚙

  • Copy The code into project

  • Set the behavior to the HomeActivity's BottomNavigationBar

The code

class BottomNavigationBehavior<V : View>(context: Context, attrs: AttributeSet) : CoordinatorLayout.Behavior<V>(context, attrs) {

	override fun onStartNestedScroll(coordinatorLayout: CoordinatorLayout, child: V, directTargetChild: View, target: View, axes: Int, type: Int): Boolean {
		return axes == ViewCompat.SCROLL_AXIS_VERTICAL
	}

	override fun onNestedPreScroll(coordinatorLayout: CoordinatorLayout, child: V, target: View, dx: Int, dy: Int, consumed: IntArray, type: Int) {
		super.onNestedPreScroll(coordinatorLayout, child, target, dx, dy, consumed, type)
		child.translationY = max(0f, min(child.height.toFloat(), child.translationY + dy))
	}

	override fun layoutDependsOn(parent: CoordinatorLayout?, child: V, dependency: View?): Boolean {
		if (dependency is Snackbar.SnackbarLayout) {
			updateSnackbar(child, dependency)
		}
		return super.layoutDependsOn(parent, child, dependency)
	}

	private fun updateSnackbar(child: View, snackbarLayout: Snackbar.SnackbarLayout) {
		if (snackbarLayout.layoutParams is CoordinatorLayout.LayoutParams) {
			val params = snackbarLayout.layoutParams as CoordinatorLayout.LayoutParams

			params.anchorId = child.id
			params.anchorGravity = Gravity.TOP
			params.gravity = Gravity.TOP
			snackbarLayout.layoutParams = params
		}
	}
}

And on the BottomNavigationView XML add this attribute

app:layout_behavior="com.joaquimley.transporteta.<_SOME_PACKAGE_>.BottomNavigationBehavior"
@JoaquimLey
Copy link
Owner Author

This might not be needed with the release of 28-alpha

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant