Skip to content

Instantly share code, notes, and snippets.

@cesco89
Last active May 6, 2018 04:34
Show Gist options
  • Save cesco89/9ddb738eef1cead8fea7 to your computer and use it in GitHub Desktop.
Save cesco89/9ddb738eef1cead8fea7 to your computer and use it in GitHub Desktop.
Listen to FragmentTransaction animation events
import android.animation.Animator;
import android.animation.AnimatorInflater;
import android.app.Fragment;
/**
* Created by francesco on 14/10/14.
*/
public class ObservableFragment extends Fragment {
private TransitionListener mListener;
@Override
public Animator onCreateAnimator(int transit, boolean enter, int nextAnim) {
if(nextAnim != 0x0) {
Animator animator = AnimatorInflater.loadAnimator(getActivity(), nextAnim);
if (mListener != null) {
animator.addListener(new Animator.AnimatorListener() {
@Override
public void onAnimationStart(Animator animation) {
mListener.onTransitionStarted();
}
@Override
public void onAnimationEnd(Animator animation) {
mListener.onTransitionEnded();
}
@Override
public void onAnimationCancel(Animator animation) {
mListener.onTransitionCancel();
}
@Override
public void onAnimationRepeat(Animator animation) {
mListener.onTransitionRepeated();
}
});
}
return animator;
}
return null;
}
public void setTransitionListener(TransitionListener listener) {
this.mListener = listener;
}
public interface TransitionListener {
public void onTransitionStarted();
public void onTransitionCancel();
public void onTransitionEnded();
public void onTransitionRepeated();
}
}
ObservableFragment f = new MyFragment();
f.setTransitionListener(new ObservableFragment.TransitionListener() {
@Override
public void onTransitionStarted() {
};
@Override
public void onTransitionCancel(){
};
@Override
public void onTransitionEnded(){
};
@Override
public void onTransitionRepeated(){
};
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment