Can't set visibility on constraint group

前端 未结 6 1534
广开言路
广开言路 2020-12-16 09:01

When i try to set the visibility of the group on button click,it doesn\'t affect the view\'s visibility.Using com.android.support.constraint:constraint-layout:1.1.0-beta4. I

相关标签:
6条回答
  • 2020-12-16 09:37

    android.support.constraint.Group has a public method

     public void updatePreLayout(ConstraintLayout container) {
       ...
     }
    

    that updates children visibilities, so calling

    dataGroup.visibility = if (visible) View.VISIBLE else View.INVISIBLE
    dataGroup.updatePreLayout(root)
    

    worked for me

    0 讨论(0)
  • 2020-12-16 09:40

    You can also simply call requestLayout method after changing Group visibility to View.INVISIBLE.

    fun makeGroupInvisible(group: Group) {
        group.visibility = View.INVISIBLE
        group.requestLayout()
    }
    

    Problem is that android.support.constraint.Group updates visibility of its members in updatePreLayout method which is called from onMeasure in ConstraintLayout.

    0 讨论(0)
  • 2020-12-16 09:47

    Just clean your project or Rebuild your project

    0 讨论(0)
  • 2020-12-16 09:54

    just add follow line you can change it . so it visible .

    group.visibility=ConstraintLayout.GONE
    
    0 讨论(0)
  • 2020-12-16 09:56

    Update: This was reported as fixed in ConstraintLayout version 2.0.0 beta 6. See bug fixes for ConstraintLayout 2.0.0 beta 6 .



    This looks like a bug to me. GONE works but INVISIBLE doesn't and I think it should. It may be worth a bug report unless someone can post where my thinking is wrong. (I am using constraint-layout:1.1.0-beta4.)

    In the meantime, here is a work-around that explicitly retrieves the ids within the group and sets the visibility of each retrieved view.

    Within MainActivity.kt

    private fun toggleLoginUI(show: Boolean) {
        if (show) {
            setGroupVisibility(mLayout, group, Group.VISIBLE)
        } else {
            setGroupVisibility(mLayout, group, Group.INVISIBLE)
        }
    }
    
    private fun setGroupVisibility(layout: ConstraintLayout, group: Group, visibility: Int) {
        val refIds = group.referencedIds
        for (id in refIds) {
            layout.findViewById<View>(id).visibility = visibility
        }
    }
    

    mLayout is the ConstraintLayout.

    Update: Here is another work-around that takes advantage of the fact that changing to/from GONE works as expected:

    private fun toggleLoginUI(show: Boolean) {
        if (show) {
            group.visibility = GONE
            group.visibility = VISIBLE
        } else {
            group.visibility = GONE
            group.visibility = INVISIBLE
        }
    }
    
    0 讨论(0)
  • 2020-12-16 09:57

    Had the same problem and nothing from above helps. My solution was to setVisibility(viewId, ConstraintSet.VISIBLE) inside constraint set and apply that to ConstraintLayout view.

    For example:

    myContstraintSet.apply {
        setVisibility(firstGroup.id, ConstraintSet.VISIBLE)
        setVisibility(secondGroup.id, ConstraintSet.GONE)
    
        connect(oneView.id, ConstraintSet.BOTTOM, R.id.secondView, ConstraintSet.TOP)
        clear(anotherView.id, ConstraintSet.TOP)
    }
    myContstraintSet.applyTo(myConstraintLayout)
    
    0 讨论(0)
提交回复
热议问题