So I have this extension function for ViewGroup
:
inline fun <reified T : View> ViewGroup.allViewsOfType(action: (T) -> Unit)
{
val views = Stack<View>()
afterMeasured {
views.addAll((0 until childCount).map(this::getChildAt))
}
while (!views.isEmpty()) {
views.pop().let {
if (it is T) action(it)
if (it is ViewGroup) {
afterMeasured {
views.addAll((0 until childCount).map(this::getChildAt))
}
}
}
}
}
And I use it like this:
tabs.allViewsOfType<Button> { Log.i("Dale", it.text.toString()) }
But somehow it doesn't work. Anything that I'm doing wrong?
Btw, tabs
is a LinearLayout
that contains three Button
s in it.