Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
208 views
in Technique[技术] by (71.8m points)

android - Can't set visibility on constraint group

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've tried setting it element-wise without problems,but no success with the group.

My MainActivity.kt

private fun toggleLoginUI(show: Boolean) {
    if (show) {
        group.visibility = VISIBLE
    } else {
        group.visibility = INVISIBLE
    }
}

fun onClick(view: View) {
    when (view.id) {
        R.id.button -> toggleLoginUI(true)
        R.id.button4 -> toggleLoginUI(false)
    }
}

My activity_main.xml

    <android.support.constraint.ConstraintLayout..

            <TextView
                android:id="@+id/textView"
... />

            <TextView
                android:id="@+id/textView2"
... />

            <Button
                android:id="@+id/button"
.../>

            <Button
                android:id="@+id/button4"
... />


            <android.support.constraint.Group
                android:id="@+id/group"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:visibility="visible"
                app:constraint_referenced_ids="textView,textView2" />
            </android.support.constraint.ConstraintLayout>
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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
    }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...