Created
November 22, 2020 08:33
-
-
Save kobeumut/3ec4bda5dc3118de30b707da71b3c0de to your computer and use it in GitHub Desktop.
Find View with tag recursively in Kotlin
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fun ViewGroup.findViewWithTagRecursively(tag: Any): List<View>? { | |
val allViews: MutableList<View> = ArrayList<View>() | |
val childCount: Int = this.childCount | |
for (i in 0 until childCount) { | |
val childView: View = this.getChildAt(i) | |
if (childView is ViewGroup) { | |
childView.findViewWithTagRecursively(tag)?.let { allViews.addAll(it) } | |
} else { | |
val tagView: Any? = childView.tag | |
if (tagView != null && tagView == tag) allViews.add(childView) | |
} | |
} | |
return allViews | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment