setCustomAnimation回调之前和之后的FragmentTransaction

时间:2013-10-27 03:43:40

标签: android android-fragments fragmenttransaction

我正在使用自定义动画替换片段,我想在动画启动时禁用某些按钮,然后在动画结束时启用。我怎么能这样做?

1 个答案:

答案 0 :(得分:26)

我建议的是创建一些基类,使所有Fragments扩展,并在其中定义一些可以重写以处理动画事件的方法。然后,覆盖onCreateAnimation()(假设您使用支持库)在动画回调上发送事件。例如:

protected void onAnimationStarted () {}

protected void onAnimationEnded () {}

protected void onAnimationRepeated () {}

@Override
public Animation onCreateAnimation (int transit, boolean enter, int nextAnim) {
    //Check if the superclass already created the animation
    Animation anim = super.onCreateAnimation(transit, enter, nextAnim);

    //If not, and an animation is defined, load it now
    if (anim == null && nextAnim != 0) {
        anim = AnimationUtils.loadAnimation(getActivity(), nextAnim);
    }

    //If there is an animation for this fragment, add a listener.
    if (anim != null) {
        anim.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart (Animation animation) {
                onAnimationStarted();
            }

            @Override
            public void onAnimationEnd (Animation animation) {
                onAnimationEnded();
            }

            @Override
            public void onAnimationRepeat (Animation animation) {
                onAnimationRepeated();
            }
        });
    }

    return anim;
}

然后,对于您的Fragment子类,只需覆盖onAnimationStarted()以禁用按钮,然后onAnimationEnded()启用按钮。