How can I disable all views inside the layout?

后端 未结 23 1292
一个人的身影
一个人的身影 2020-11-28 08:46

For example I have:



        
23条回答
  •  無奈伤痛
    2020-11-28 09:24

    My two cents function without recursion

        package io.chord.ui.utils
    
    import android.view.View
    import android.view.ViewGroup
    import androidx.core.view.forEach
    
    class ViewUtils
    {
        companion object
        {
            fun setViewState(view: View, state: Boolean)
            {
                var depth = 0
                val views: MutableMap> = mutableMapOf()
                views[depth] = mutableListOf(view)
    
                while(true)
                {
                    val currentViews = views[depth]
                    val nextViews = mutableListOf()
    
                    currentViews!!.forEach { view ->
                        if(view is ViewGroup)
                        {
                            view.forEach { children ->
                                nextViews.add(children)
                            }
                        }
                    }
    
                    if(nextViews.size == 0)
                    {
                        break
                    }
    
                    depth++
    
                    views[depth] = nextViews
                }
    
                views.flatMap {
                    it.value
                }.forEach {
                    it.isEnabled = state
                }
            }
        }
    }
    

提交回复
热议问题