Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
fun View?.isDescendant(container: ViewGroup): Boolean {
this ?: return false
var p: ViewParent? = parent
while (p != null) {
if (p === container) {
return true
}
p = p.parent
}
return false
}
fun Fragment.clickViewWithTag(@StringRes tag: Int) = (view as ViewGroup).clickViewWithTag(tag)
fun ViewGroup.clickViewWithTag(@StringRes tag: Int) = context.getString(tag).let {
findViewWithTag<View>(it).parentView?.performClick() == true
}
fun <V : View> V.setOnLayoutReadyListener(callback: V.() -> Unit) {
viewTreeObserver.addOnGlobalLayoutListener(object : ViewTreeObserver.OnGlobalLayoutListener {
override fun onGlobalLayout() {
callback(this@setOnLayoutReadyListener)
viewTreeObserver.removeOnGlobalLayoutListener(this)
}
})
}
fun ViewGroup.unfocusViewsRecursively() {
children
.onEach { it.isFocusable = false }
.filterIsInstance<ViewGroup>()
.forEach { it.unfocusViewsRecursively() }
}
val ViewGroup.children get() = (0 until childCount).map { getChildAt(it) }
val View.inflater get() = LayoutInflater.from(context)
fun ViewGroup.hasChildFocus(): Boolean {
if (isFocused) return true
val childFocus = children
.filterIsInstance<ViewGroup>()
.map { it.hasChildFocus() }
.any { it }
return findFocus() != null || childFocus
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.