Skip to content

Instantly share code, notes, and snippets.

@yongjhih
Last active October 21, 2023 11:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yongjhih/dc64112cd1fea5d3ad586369da9125cd to your computer and use it in GitHub Desktop.
Save yongjhih/dc64112cd1fea5d3ad586369da9125cd to your computer and use it in GitHub Desktop.
inline fun <reified T : View> ViewGroup.childrenByType(): Sequence<T> = descendantsBreadth.filterIsInstance<T>()
val ViewGroup.descendantsBreadth: Sequence<View>
get() = sequence {
val queue: Queue<View> = LinkedList()
this@descendantsBreadth.forEach { child -> queue.offer(child) }
var child = queue.poll()
while (child != null) {
yield(child)
if (child is ViewGroup) {
for (i in 0 until child.childCount) {
queue.offer(child.getChildAt(i))
}
}
child = queue.poll()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment