android-ktx
android-ktx copied to clipboard
Allow to perform action on view and its descendents, including a filter to keep digging
Suppose you wish to perform an operation on a view and all its descendants.
I've made some functions in the past that resemble this. For example, I've made one to set views enabled/disabled:
/**
* sets the view and all of its descendents to be enabled/disabled .
*/
@JvmStatic
fun setEnabledToViewAndItsDescendants(v: View?, enabled: Boolean) {
if (v == null)
return
v.isEnabled = enabled
if (v is ViewGroup) {
val viewGroups = ArrayList<ViewGroup>()
viewGroups.add((v as ViewGroup?)!!)
while (!viewGroups.isEmpty()) {
val viewGroup = viewGroups.removeAt(0)
val childCount = viewGroup.childCount
for (i in 0 until childCount) {
val childView = viewGroup.getChildAt(i)
childView.isEnabled = enabled
if (childView is ViewGroup)
viewGroups.add(childView)
}
}
}
}
Other cases might be to set clickable, checked, etc...
What I request, is that instead of being so specific, we would have a single function that would require us to choose which operation to perform on the view, and if we should dig deeper inside the ViewGroup to perform the operations there too.
This seem too specific.
Why not use ViewGroup.forEach
?
@MGaetan89 I don't think what you offer is recursive. I wrote "descendents" . Not children