渲染之前触发ViewPager片段中嵌套片段的动画 [英] Animation of nested fragment within ViewPager fragment is triggered before render

查看:97
本文介绍了渲染之前触发ViewPager片段中嵌套片段的动画的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个ViewPager片段

class PagerFragment() : Fragment() {
  override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
      super.onViewCreated(view, savedInstanceState)

      val pagerView = view as ViewPager
      val adapter = PagerAdapter(childFragmentManager, items)
      pagerView.adapter = adapter
  }
}

使用以下适配器

class PagerAdapter(val fm: FragmentManager, val items: ArrayList<Item>)
    : FragmentStatePagerAdapter(fm) {

    override fun getItem(position: Int): Fragment {
        val item = items[position]
        return NestedFragment.newInstance(item)
    }

    override fun getCount(): Int {
        return  items.size
    }
}

通过以下方式嵌套片段并触发一些动画onViewCreated:

which nests fragments with a few animations triggered onViewCreated the following way:

class NestedFragment() : Fragment() {
  override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
      super.onViewCreated(view, savedInstanceState)

      val one = AnimationUtils.loadAnimation(context, R.anim.one)
      val two = AnimationUtils.loadAnimation(context, R.anim.two)
      val three = AnimationUtils.loadAnimation(context, R.anim.three)

      view.findViewById<ImageView>(R.id.pic).startAnimation(one)
      view.findViewById<ConstraintLayout>(R.id.content).startAnimation(two)
      view.findViewById<ImageView>(R.id.img).startAnimation(three)
  }
}

但是经常在屏幕上显示嵌套片段时,动画已经开始甚至结束了:(

However often by the time the nested fragment is showed on the screen the animation has already started or even ended :(

是否有一种方法可以确定片段何时确实在设备的屏幕上呈现? 当然,也欢迎任何其他有用的建议来解决该问题.

Is there a way to determine when the fragment is indeed rendered on the screen of the device? Of course any other useful suggestions to overcome the issue are welcome as well.

推荐答案

这是对我有用的.

class NestedFragment() : Fragment() {
  override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
      super.onViewCreated(view, savedInstanceState)

      val one = AnimationUtils.loadAnimation(context, R.anim.one)
      val two = AnimationUtils.loadAnimation(context, R.anim.two)
      val three = AnimationUtils.loadAnimation(context, R.anim.three)
      view.post(Runnable {
        /* 
          If the animation doesn't trigger you may try to hide 
          elements visibility and reveal them before animation starts
        */
        view.findViewById<ImageView>(R.id.pic).startAnimation(one)
        view.findViewById<ConstraintLayout>(R.id.content).startAnimation(two)
        view.findViewById<ImageView>(R.id.img).startAnimation(three)
      })
  }
}

如果动画仍然没有触发,您可以尝试隐藏视图的可见性,并在动画之前显示它们.

If the animation still doesn't trigger you may try to hide views visibility and reveal them right before the animation.

这篇关于渲染之前触发ViewPager片段中嵌套片段的动画的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆