如何动画片段删除

hug*_*goc 33 animation android fragment

我想动画片段的删除.

我试过了:

getSupportFragmentManager().beginTransaction()
    .setCustomAnimations(R.anim.push_down_in, R.anim.push_up_out)
    .remove(myFragment)
    .commit();
Run Code Online (Sandbox Code Playgroud)

但片段就消失了.

我注意到out动画只播放'replace',所以我尝试用这样的空片段替换片段:

getSupportFragmentManager()
    .beginTransaction()
    .setCustomAnimations(R.anim.push_down_in, R.anim.push_up_out)
    .replace(viewId, new Fragment())
.commit();
Run Code Online (Sandbox Code Playgroud)

但它仍然只是消失了.

那么,我如何动画片段的删除?

hug*_*goc 20

我想到了.

退出视图在输入视图的画布上设置动画,因此如果没有输入画布,则没有动画画布.

为了显示动画,我必须始终使用替换并使用与退出的相同大小的输入片段.动画结束后,我将新片段的视图设置为已消失.


zol*_*ish 16

当我遇到类似的问题时,我就看到了这一点,并且我认为Id会快速记下.

我认为您应该为当前片段视图设置动画,而不是创建虚拟片段以替换现有片段.动画结束后,您只需删除片段即可.

这就是我做到的方式:

final FragmentActivity a = getSherlockActivity();

if (a != null) {
    //Your animation
    Animation animation = AnimationUtils.loadAnimation(a, R.anim.bottom_out);
    animation.setDuration(getResources().getInteger(android.R.integer.config_shortAnimTime));

    //You can use AnimationListener, MagicAnimationListener is simply a class extending it.
    animation.setAnimationListener(new MagicAnimationListener() {
        @Override
        public void onAnimationEnd(Animation animation) {
            //This is the key, when the animation is finished, remove the fragment.
            try {
                FragmentTransaction ft = a.getSupportFragmentManager().beginTransaction();
                ft.remove(RestTimerFragment.this);
                ft.commitAllowingStateLoss();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });

    //Start the animation.  
    getView().startAnimation(animation);
}
Run Code Online (Sandbox Code Playgroud)

  • 2020 年阅读《SherlockActivity》时,我不得不咧嘴一笑。过去的美好时光:-) (2认同)

Ant*_*wan 9

您可以通过将此自定义动画设置为fragmentTransaction来为删除设置动画

        fragmentTransaction.setCustomAnimations(R.anim.right_in, R.anim.defff,R.anim.defff,R.anim.right_out);
Run Code Online (Sandbox Code Playgroud)

第三和第四个参数用于移除碎片

  • 问题是这件事不起作用 (7认同)