首页 > 解决方案 > 如果没有活动的第一个片段中的处理程序,动画将无法工作

问题描述

我在正常状态下不起作用的片段中使用了动画。我应该在 handlers 中使用它们。我正在使用片段导航,这是出现此问题的活动的第一个片段。其他片段工作正常。

class HomeFragment : BaseFragment<FragmentHomeBinding>() {
    override fun onResume() {
    super.onResume()
    if (firstTime) {

        Handler().postDelayed({
            binding.finalHomeFragment.setTransitionDuration(1)
            binding.finalHomeFragment.transitionToEnd()
            binding.view.visibility = View.GONE
            binding.buttonLayout.visibility = View.GONE
            setTransition()
        }, 0)
        firstTime = false
    } else {
        Handler().postDelayed({
            binding.finalHomeFragment.setTransitionDuration(1500)
            binding.finalHomeFragment.transitionToEnd()
        }, 0)
    }


    private fun setTransition() {
        val autoTransition = AutoTransition()
        autoTransition.duration = 500
        TransitionManager.beginDelayedTransition(binding.container, 
autoTransition)
        val set = ConstraintSet()
        set.clone(binding.container)
        set.setVisibility(binding.buttonLayout.id, View.VISIBLE)
        set.setVisibility(binding.view.id, View.VISIBLE)
        set.applyTo(binding.container)
    }

}

我使用 MotionLayout 和 TransitionManager 制作动画

标签: androidanimationkotlin

解决方案


推荐阅读