constraintlayout.widget.Group动画不能与TransitionManager一起使用

时间:2019-06-26 09:23:29

标签: android android-animation android-constraintlayout

没有人知道为什么用TransitionManager对constraintlayout.widget.Group进行可见性动画设置不起作用吗?这个小部件不是为这类事情制作的吗?

从组中分离视图后隐藏或显示项目是可行的

        <androidx.constraintlayout.widget.Group
            android:id="@+id/cardHeadersGroup"
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:visibility="invisible"
            app:constraint_referenced_ids="cardSystemHeader,cardSimpleHeader,cardCombinedHeader"
            app:layout_constraintBottom_toBottomOf="@+id/cardCombinedHeader"
            app:layout_constraintEnd_toEndOf="@+id/cardSystemHeader"
            app:layout_constraintStart_toStartOf="@+id/cardSimpleHeader"
            app:layout_constraintTop_toTopOf="parent"
            tools:visibility="visible"/>
 val headersGroup = binding.cardHeadersGroup

        val slideIn = Slide()
        slideIn.slideEdge = Gravity.BOTTOM
        slideIn.mode = Slide.MODE_IN
        slideIn.addTarget(headersGroup)

        TransitionManager.beginDelayedTransition(binding.root as ViewGroup, slideIn)

        headersGroup.visibility = VISIBLE

2 个答案:

答案 0 :(得分:1)

我最近一直在与TransitionManagerConstraintLayout.Group一起工作,发现它存在很多问题。

最终,我决定转储整个ConstraintLayout.Group,并创建了一个代码内的AnimationGroup(类似于in-xml ConstraintLayout.Group):

class AnimationGroup(vararg val views: View) {
    var visibility: Int = View.INVISIBLE
        set(value) {
            views.forEach {
                it.visibility = value
            }
            field = value
        }
}

Transition的扩展功能:

private fun Transition.addTarget(animationGroup: AnimationGroup) {
    animationGroup.views.forEach { viewInGroup ->
        this.addTarget(viewInGroup)
    }
}

这样,您可以执行以下操作(几乎完全相同的代码,但是xml更简单-否ConstraintLayout.Group):

val headersGroup = AnimationGroup(
    binding.cardSystemHeader, 
    binding.cardSimpleHeader, 
    binding.cardCombinedHeader
)

val slideIn = Slide()
slideIn.slideEdge = Gravity.BOTTOM
slideIn.mode = Slide.MODE_IN
slideIn.addTarget(headersGroup)

TransitionManager.beginDelayedTransition(binding.root as ViewGroup, slideIn)

headersGroup.visibility = VISIBLE

答案 1 :(得分:1)

我们也可以通过简单的扩展函数提取Group的引用视图:

fun Group.getReferencedViews() = referencedIds.map { rootView.findViewById<View>(it) }