android-ktx icon indicating copy to clipboard operation
android-ktx copied to clipboard

Allow to perform action on view and its descendents, including a filter to keep digging

Open AndroidDeveloperLB opened this issue 6 years ago • 2 comments

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.

AndroidDeveloperLB avatar Feb 06 '18 13:02 AndroidDeveloperLB

This seem too specific. Why not use ViewGroup.forEach?

MGaetan89 avatar Feb 06 '18 13:02 MGaetan89

@MGaetan89 I don't think what you offer is recursive. I wrote "descendents" . Not children

AndroidDeveloperLB avatar Feb 06 '18 13:02 AndroidDeveloperLB