How can I disable all views inside the layout?

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

For example I have:



        
23条回答
  •  日久生厌
    2020-11-28 09:24

    Details

    • Android studio 3.1.4
    • Kotlin 1.2.70
    • checked in minSdkVersion 19

    Solution

    fun View.forEachChildView(closure: (View) -> Unit) {
        closure(this)
        val groupView = this as? ViewGroup ?: return
        val size = groupView.childCount - 1
        for (i in 0..size) {
            groupView.getChildAt(i).forEachChildView(closure)
        }
    }
    

    Usage

    val layout = LinearLayout(context!!)
    layout.forEachChildView {  it.isEnabled = false  }
    
    val view = View(context!!)
    view.forEachChildView {  it.isEnabled = false  }
    
    val fragment = Fragment.instantiate(context, "fragment_id")
    fragment.view?.forEachChildView {  it.isEnabled = false  }
    

提交回复
热议问题