【问题标题】:Animate removal of an Fragment动画移除片段
【发布时间】:2016-02-06 15:59:35
【问题描述】:

在一个应用程序中,我尝试为片段的移除制作动画。

transaction.remove(fragmentVideo).remove(fragmentProgressBar).replace(R.id.content_pane_calling, endFragment);
transaction.setCustomAnimations(R.anim.slide_in, R.anim.slide_up);
transaction.commit();

框架完全忽略了这一点。片段本身被移除,但视觉效果并不好。任何FragmentTransaction#replace 都适用于这些动画。我正在使用SupportLibrary v23.1.

感谢您帮助我:)

【问题讨论】:

    标签: android android-fragments android-animation


    【解决方案1】:

    必须在 transaction.remove()/add()/replace() 之前调用 transaction.setCustomAnimation(),否则动画将永远不会运行。

    所以你的代码看起来像:

    transaction.setCustomAnimations(R.anim.slide_in, R.anim.slide_up);
    transaction.remove(fragmentVideo).remove(fragmentProgressBar).replace(R.id.content_pane_calling, endFragment);
    transaction.commit();
    

    【讨论】:

    • 嗯,好的。似乎在stackoverflow.com/questions/13982240/… 之前已经提出过这个问题,并且当动画在传入视图上运行时,您实际上无法为片段的移除设置动画。我链接到的问题有一些关于如何解决这个问题的想法。
    【解决方案2】:

    这是我曾经面临的一个很常见的问题。我在决定使用 Fragments 时非常小心,因为它们对于 UI 动画非常不灵活。您可能会在这里找到答案:

    http://daniel-codes.blogspot.com/2013/09/smoothing-performance-on-fragment.html

    您还可以为您的片段设置动画并在完成动画后执行移除事务,以适当的顺序分别执行这两项操作。

    //Pseudo Code
    
    Animation anim = ... //this animates your fragment view
    anim.setAnimationListener(new AnimationListener() {
    
        void onAnimFinish() {
            transaction.remove(fragmentVideo).remove(fragmentProgressBar).replace(R.id.content_pane_calling, endFragment);
            transaction.setCustomAnimations(R.anim.slide_in, R.anim.slide_up);
            transaction.commit()
            getFragmentManager().executePendingTransactions();
        }
    
    })
    

    【讨论】:

      猜你喜欢
      • 2012-12-08
      • 2015-04-29
      • 2017-05-18
      • 2011-12-30
      • 2023-04-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-05-28
      相关资源
      最近更新 更多