片段交易animation完成后执行动作

我想在animation完成后设置button的可见性。

这就是所谓的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) 

有什么办法可以附加一个监听器来知道我的片段何时离开?

@nmw在他的答案中实现的animation添加在API Level 11中,并且不会与Android支持库实现的Fragments一起使用。

要监听片段animation事件,我扩展了支持库的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; } } 

您需要inheritanceFragment并覆盖onCreateAnimator,然后您可以从XML加载这些animation并将侦听器附加到这些animation。

例如

 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; } 

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

只需简单地扩展MenuFragment并设置监听器来获得之后执行的callback。

 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(); } 

}

我必须在Xamarin中这样做。 我的情况是,一旦片段animation结束,我需要一个callback。 下面是我如何使其工作没有任何闪烁(这是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会覆盖你的监听器,并且callback不会被触发。