Last active
May 15, 2023 16:48
-
-
Save edudobay/19755b871b10101fa5b7a3c610c00eae to your computer and use it in GitHub Desktop.
Behavior for CoordinatorLayout that handles views fixed at the bottom
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.github.edudobay.android | |
import android.content.Context | |
import android.support.design.widget.AppBarLayout | |
import android.support.design.widget.CoordinatorLayout | |
import android.support.v4.view.ViewCompat | |
import android.support.v7.widget.Toolbar | |
import android.util.AttributeSet | |
import android.view.Gravity | |
import android.view.View | |
import android.view.ViewGroup | |
import android.view.ViewGroup.LayoutParams.MATCH_PARENT | |
fun ViewGroup.children(start: Int = 0, end: Int = childCount): Sequence<View> = | |
(start..end).asSequence().mapNotNull { i -> getChildAt(i) } | |
infix fun Int.andNot(other: Int): Int = and(other.inv()) | |
fun Int.toggleBits(other: Int, enabled: Boolean = true) = | |
if (enabled) this.or(other) else this.andNot(other) | |
/* | |
* Converted to Kotlin by Eduardo Dobay (2016-12-05) | |
* - Added: depend on views anchored to bottom, and subtract them from available height | |
* | |
* Original version by Maciej Karas | |
* https://gist.github.com/MaciejKaras/02bff315f00b87d80467a470424f22c3 | |
*/ | |
@Suppress("unused") | |
class FixedBottomAwareScrollViewBehavior : AppBarLayout.ScrollingViewBehavior { | |
constructor() : super() | |
constructor(context: Context, attrs: AttributeSet) : super(context, attrs) | |
override fun layoutDependsOn(parent: CoordinatorLayout, child: View, dependency: View): Boolean = | |
isViewAnchoredToBottom(dependency) || super.layoutDependsOn(parent, child, dependency) | |
fun isViewAnchoredToBottom(view: View) = | |
(view.layoutParams as? CoordinatorLayout.LayoutParams)?.gravity == Gravity.BOTTOM | |
override fun onMeasureChild( | |
parent: CoordinatorLayout, child: View, | |
parentWidthMeasureSpec: Int, widthUsed: Int, | |
parentHeightMeasureSpec: Int, heightUsed: Int | |
): Boolean { | |
val dependencies = parent.getDependencies(child) | |
if (child.layoutParams.height == MATCH_PARENT && !dependencies.isEmpty()) { | |
var availableHeight = View.MeasureSpec.getSize(parentHeightMeasureSpec) | |
if (availableHeight == 0) { | |
availableHeight = parent.height | |
} | |
// Subtract height of views anchored to bottom | |
availableHeight -= dependencies.filter { isViewAnchoredToBottom(it) }.sumBy { it.measuredHeight } | |
val availableHeightMeasureSpec = View.MeasureSpec.makeMeasureSpec(availableHeight, View.MeasureSpec.AT_MOST) | |
val appBar = findFirstAppBarLayout(dependencies) | |
if (appBar != null && ViewCompat.isLaidOut(appBar)) { | |
val height = availableHeight - appBar.measuredHeight | |
var newHeightMeasureSpec = View.MeasureSpec.makeMeasureSpec(height, View.MeasureSpec.AT_MOST) | |
parent.onMeasureChild(child, parentWidthMeasureSpec, widthUsed, newHeightMeasureSpec, heightUsed) | |
val childContentHeight = child.measuredHeight | |
if (childContentHeight <= height) { | |
updateToolbar(parent, appBar, parentWidthMeasureSpec, widthUsed, availableHeightMeasureSpec, heightUsed, false) | |
newHeightMeasureSpec = View.MeasureSpec.makeMeasureSpec(height, View.MeasureSpec.EXACTLY) | |
parent.onMeasureChild(child, parentWidthMeasureSpec, widthUsed, newHeightMeasureSpec, heightUsed) | |
return true | |
} else { | |
updateToolbar(parent, appBar, parentWidthMeasureSpec, widthUsed, availableHeightMeasureSpec, heightUsed, true) | |
return super.onMeasureChild(parent, child, parentWidthMeasureSpec, widthUsed, availableHeightMeasureSpec, heightUsed) | |
} | |
} | |
} | |
return super.onMeasureChild(parent, child, parentWidthMeasureSpec, widthUsed, parentHeightMeasureSpec, heightUsed) | |
} | |
private fun View.getContentHeight(): Int = | |
(this as? ViewGroup)?.children()?.sumBy { it.measuredHeight } ?: measuredHeight | |
private fun findFirstAppBarLayout(views: List<View>): AppBarLayout? = | |
views.firstOrNull { view -> view is AppBarLayout } as? AppBarLayout | |
private fun updateToolbar( | |
parent: CoordinatorLayout, appBar: AppBarLayout, | |
parentWidthMeasureSpec: Int, widthUsed: Int, | |
parentHeightMeasureSpec: Int, heightUsed: Int, | |
scroll: Boolean | |
) { | |
toggleToolbarScroll(appBar, scroll) | |
appBar.forceLayout() | |
parent.onMeasureChild(appBar, parentWidthMeasureSpec, widthUsed, parentHeightMeasureSpec, heightUsed) | |
} | |
private fun toggleToolbarScroll(appBar: AppBarLayout, scroll: Boolean) { | |
for (child in appBar.children().filter { child -> child is Toolbar }) { | |
val params = child.layoutParams as AppBarLayout.LayoutParams | |
params.scrollFlags = params.scrollFlags.toggleBits(AppBarLayout.LayoutParams.SCROLL_FLAG_SCROLL, scroll) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment