美文网首页
Fragment过渡动画

Fragment过渡动画

作者: 小菜鸟程序媛 | 来源:发表于2018-06-27 11:36 被阅读217次

原本的代码:

supportFragmentManager.beginTransaction()
    .replace(R.id.home_content, fragment)
    .addToBackStack(tag)
    .commit()

于是我查找了过渡的API然后添加一些共享元素,在这种情况下,共享元素是包含海报的方形图像。

supportFragmentManager.beginTransaction()
    .replace(R.id.home_content, fragment)
    .addToBackStack(tag)
    .apply{
        for (view in sharedElementViews){
            addSharedElement(view)
        }
    }

然后在进入的fragment上设置一个共享元素转换,在这个示例中,我使用了 ChangedBounds,因为view只是从A点移动到B点并改变大小。

class GridFragment : Fragment() {
  override fun onCreate(savedInstanceState: Bundle?){
    super.onCreate(savedInstanceState)
    sharedElementEnterTransition = ChangeBounds()
  }
}

上面的示例在进入时根本不起作用,但在返回过渡时工作的很好,这只是一个开始。

推迟

推迟转换直到view准备好(布局,数据加载等)。

因此在上面的代码中加入推迟的代码

override fun onViewCreated(view: View, icicle: Bundle?) {
  // View is created so postpone the transition for now
  postponeEnterTransition()

  viewModel.liveList.observe(this) {
    controller.setList(it)
    // Data is loaded so we’re ready to start our transition
    startPostponedEnterTransition()
  }
}

我们必须为进入和退出fragment都要设置此操作,以便进入和退出都能按预期进行。

重新排序

必须启用fragment事务中的重新排序才能使延迟的fragment过渡生效。

supportFragmentManager.beginTransaction()
    .setReorderingAllowed(true)
    .replace(R.id.home_content, fragment)
    .addToBackStack(tag)
    .apply{
      for (view in sharedElementViews){
        addSharedElement(view)
      }
    }
    .commit()

等待你的父布局

override fun onViewCreated(view: View, icicle: Bundle?){
  postponeEnterTransition()
  
  viewModel.liveList.observe(this) {
    controller.setList(this)
    (view?.parent as? ViewGroup)?.doOnPreDraw {
      startPostponedEnterTransition()
    }
  }
}

相关文章

  • Fragment过渡动画

    原本的代码: 于是我查找了过渡的API然后添加一些共享元素,在这种情况下,共享元素是包含海报的方形图像。 然后在进...

  • Fragment自定义过渡动画

    原文地址:https://www.thedroidsonroids.com/blog/android/workca...

  • 11.自定义过渡动画

    11.1 问题 应用程序需要自定义Activity切换或Fragment切换时产生的过渡动画。 11.2 解决方案...

  • 属性动画和Activity、Fragment过渡动画等

    主题是关于动画的,但是不是什么动画的内容都包括。先泛泛的介绍一下,然后详细的介绍一下翻代码找见的一个好玩的动画的使...

  • JS中的动画事件和过渡事件

    动画事件 动画事件demo 过渡动画事件 过渡动画事件demo

  • CSS3过渡动画

    过渡动画 transition属性简介transition是网页上的过渡动画,变化的逐渐过渡效果,简称过渡动画!列...

  • 过渡与动画

    @(HTML5)[过渡, 动画] [TOC] 五、过渡与动画 过渡 transition-property:过渡属...

  • 动画core animation

    type 动画过渡类型subtype 动画过渡方向

  • Android过渡动画Scene and Transition(

    Android场景过渡——Scene and Transition(一) 场景过渡动画 场景过渡动画是指以动画的形...

  • 016 过渡及动画

    过渡与动画 一、过渡 1、过渡属性 二、动画 1、动画属性 2、动画体 v_hint:动画属性设置给指定选择器标签...

网友评论

      本文标题:Fragment过渡动画

      本文链接:https://www.haomeiwen.com/subject/kwpkyftx.html