Get all child views inside LinearLayout at once

前端 未结 6 2178
野性不改
野性不改 2020-11-28 04:39

I have a LinearLayout, which contains several child TextViews. How can I get child views of that LinerLayout using a loop?

6条回答
  •  日久生厌
    2020-11-28 05:30

    Get all views of a view plus its children recursively in Kotlin:

    private fun View.getAllViews(): List {
        if (this !is ViewGroup || childCount == 0) return listOf(this)
    
        return children
                .toList()
                .flatMap { it.getAllViews() }
                .plus(this as View)
    }
    

提交回复
热议问题