Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
A fragment container enabling the use of android:fitsSystemWindows in fragment layouts
class WindowInsetsFrameLayout @JvmOverloads constructor(
context: Context,
attrs: AttributeSet? = null,
defStyleAttr: Int = 0
) : FrameLayout(context, attrs, defStyleAttr) {
init {
setOnHierarchyChangeListener(object : OnHierarchyChangeListener {
override fun onChildViewAdded(
parent: View,
child: View
) {
requestApplyInsets()
}
override fun onChildViewRemoved(
parent: View,
child: View
) {
}
})
}
override fun dispatchApplyWindowInsets(insets: WindowInsets): WindowInsets {
if (!insets.isConsumed) {
val count = childCount
for (i in 0 until count) {
val freshInsets = WindowInsets(insets)
getChildAt(i)?.let { safeChild ->
if (safeChild.visibility != View.GONE) safeChild.dispatchApplyWindowInsets(freshInsets)
}
}
}
return insets
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment