Skip to content

Instantly share code, notes, and snippets.

@hrules6872
Created December 2, 2019 10:03
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hrules6872/ec8afd0ac2508b35ed6ea25f8deebac3 to your computer and use it in GitHub Desktop.
Save hrules6872/ec8afd0ac2508b35ed6ea25f8deebac3 to your computer and use it in GitHub Desktop.
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