setCustomAnimation回调之前和之后的FragmentTransaction

Chr*_*sco 8 android android-fragments fragmenttransaction

我正在使用自定义动画来替换片段,我想在动画开始时禁用一些按钮,然后在动画结束时启用.我怎样才能做到这一点?

kco*_*ock 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;
}
Run Code Online (Sandbox Code Playgroud)

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

  • 这不适用于Slide或Explode等材质转换,因为`anim`始终为null. (3认同)