好的,我真的做到了。这是相当多的工作,但它已经完成了!
我有一个扩展片段的BaseFragment类。
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();
Log.i("TESTTAG", "ANIM STARTED: " );
}
@Override
public void onAnimationEnd (Animation animation) {
Log.i("TESTTAG", "ANIM END: " );
onAnimationEnded();
}
@Override
public void onAnimationRepeat (Animation animation) {
onAnimationRepeated();
}
});
}
return anim;
}
在这里我添加了这个。这使得可以覆盖
onAnimationStart
和
OnAnimation结束
@Override
protected void onAnimationStarted() {
super.onAnimationStarted();
isAnimationDone = false;
}
@Override
protected void onAnimationEnded() {
isAnimationDone = true;
super.onAnimationEnded();
}
在这里,我设置了一个布尔值,以查看动画是否已实际结束。
反压
在fragment中,我添加了以下代码,fragment是我创建的用于捕捉backPress事件的自定义方法。
@Override
public void doBack() {
if (isAnimationDone){
getActivity().getSupportFragmentManager().popBackStack();
}
}