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