I am attempting to create a generic extension function that can walk an Android view hierarchy and return the first occurence of a view of a specific type.
The idea would be to invoke the extension as follows (to find the first occurence of a Toolbar
inside of parentView
):
val someView = parentView.findFirstChildRecursive<Toolbar>()
Unfortunately the code below doesn't compile. I guess Kotlin is not happy about having recursive inline functions, but I can't use a reified type without inlining the function.
inline fun <reified T> View.findFirstChildRecursive(): T? {
when (this) {
is T -> return this
is ViewGroup -> {
for (i in 0 until childCount) {
getChildAt(i).findFirstChildRecursive<T>()?.let { return it }
}
}
}
return null
}
I'm a little bit of a Kotlin newbie, so I was hoping someone could explain why or propose a good solution?