Android | Get all children elements of a ViewGroup

后端 未结 7 1612
星月不相逢
星月不相逢 2020-12-08 02:56

getChildAt(i) on gets only the direct children of a ViewGroup, is it possible to access to all children without doing nested loops?

7条回答
  •  暖寄归人
    2020-12-08 02:56

    I recently had the same issue and solved it using Kotlin's extension functions like this:

    fun ViewGroup.getAllChildren(): List {
        val children = ArrayList()
        for (i in 0 until this.childCount) {
            children.add(this.getChildAt(i))
        }
        return children
    }
    

提交回复
热议问题