片段事务动画完成后执行操作

时间:2012-06-20 13:12:18

标签: android android-fragments android-animation

我想在动画结束后设置按钮可见性。

这就是调用动画的原因:

android.support.v4.app.FragmentTransaction fAnimation = this.getActivity().getSupportFragmentManager().beginTransaction();
fAnimation.setCustomAnimations(android.R.anim.slide_in_left, R.anim.pull_out_to_left);
if (this.isVisible()) {
    fAnimation.hide(this);
    fAnimation.commit();
}

// code that will be executed when the fragment is gone (after the animation is over)

有没有办法让听众知道我的片段何时消失?

5 个答案:

答案 0 :(得分:61)

@nmw在他的答案中实现的动画师在API等级11中添加,并且不适用于Android支持库实现的片段。

为了收听Fragment动画事件,我扩展了支持库的Fragment类并覆盖onCreateAnimation,将自定义AnimationListener附加到返回的Animation对象:

public class MyFragment extends android.support.v4.app.Fragment {

    private static final String TAG = "MyFragment";

    @Override
    public Animation onCreateAnimation(int transit, boolean enter, int nextAnim) {

        Animation anim = AnimationUtils.loadAnimation(getActivity(), nextAnim);

        anim.setAnimationListener(new AnimationListener() {

            @Override
            public void onAnimationStart(Animation animation) {
                Log.d(TAG, "Animation started.");
                // additional functionality 
            }

            @Override
            public void onAnimationRepeat(Animation animation) {
                Log.d(TAG, "Animation repeating.");
                // additional functionality
            }

            @Override
            public void onAnimationEnd(Animation animation) {
                Log.d(TAG, "Animation ended.");
                // additional functionality
            }
        });

        return anim;
    }
}

答案 1 :(得分:16)

您需要子类化Fragment并覆盖onCreateAnimator,然后您可以从XML加载这些动画并将侦听器附加到它们。

E.g。

public class MyFragment extends Fragment
{
    @Override
    public Animator onCreateAnimator(int transit, boolean enter, int nextAnim)
    {
        final int animatorId = (enter) ? R.anim.in_anim : R.anim.out_anim;
        final Animator anim = AnimatorInflater.loadAnimator(getActivity(), animatorId);
        anim.addListener(new AnimatorListenerAdapter()
        {
            @Override
            public void onAnimationStart(Animator animation)
            {
                ...
            }

            @Override
            public void onAnimationEnd(Animator animation)
            {
               ...
            }
        });

        return anim;
   }    

答案 2 :(得分:7)

结合上面的答案是我成功使用支持库片段的示例。

只需扩展MenuFragment并设置监听器即可获得之后执行的回调。

public class MenuFragment extends Fragment {

private WeakReference<OnMenuClosedListener> onMenuClosedListener;

@Override
public Animation onCreateAnimation(int transit, boolean enter, int nextAnim) {
    Animation anim = null;
    if (enter) {
        anim = AnimationUtils.loadAnimation(getActivity(), R.anim.anim_slide_in_top);
    } else {
        anim = AnimationUtils.loadAnimation(getActivity(), R.anim.anim_menu_slide_out_top);
        anim.setAnimationListener(new AnimationListener() {
            @Override public void onAnimationStart(Animation animation) {
            }
            @Override public void onAnimationRepeat(Animation animation) {
            }
            @Override public void onAnimationEnd(Animation animation) {
                onMenuClosed();
            }
        });
    }

    // NOTE: the animation must be added to an animation set in order for the listener
    // to work on the exit animation
    AnimationSet animSet = new AnimationSet(true);
    animSet.addAnimation(anim);

    return animSet;
}

private void onMenuClosed() {
    if (this.onMenuClosedListener != null) {
        OnMenuClosedListener listener = this.onMenuClosedListener.get();
        if (listener != null) {
            listener.onMenuClosed();
        }
    }
}

public void setOnMenuClosedListener(OnMenuClosedListener listener) {
    this.onMenuClosedListener = new WeakReference<MenuFragment.OnMenuClosedListener>(listener);
}

/**
 * Callback for when the menu is closed.
 */
public static interface OnMenuClosedListener {

    public abstract void onMenuClosed();

}

}

答案 3 :(得分:3)

已添加到API 26(和支持库)中,您可以使用

 FragmentTransaction runOnCommit (Runnable runnable);

例如:

FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.setCustomAnimations(R.anim.enter_from_right, R.anim.exit_from_left);
ft.show(YourFragment);
ft.commit();
ft.runOnCommit(() -> Your_Action_Here);

答案 4 :(得分:0)

我必须在Xamarin中这样做。我的情况是片段动画结束后我需要回调。这是我如何使其工作没有任何闪烁(这是C#/ Xamarin):

    public override Animation OnCreateAnimation(int transit, bool enter, int nextAnim)
    {
        Animation anim = base.OnCreateAnimation(transit, enter, nextAnim);

        if (anim == null && nextAnim != 0) {
            anim = AnimationUtils.LoadAnimation(Activity, nextAnim);
        }

        anim.SetAnimationListener(this);
        return anim;
    }

    public void OnAnimationEnd(Animation animation)
    {
    }

    public void OnAnimationRepeat(Animation animation)
    {
    }

    public void OnAnimationStart(Animation animation)
    {
    }

注意:

  • 父片段必须实现Animation.IAnimationListener
  • 您必须返回AnimationSet,否则FragmentManager会覆盖您的听众并且回调不会被解雇。