Ответ 1
Метод onCreateAnimator
нечетный.
Прототип, который я видел, таков:
public Animator onCreateAnimator(int transit, boolean enter, int nextAnim)
int transit
- тип перехода, как сказано выше.
boolean enter
- true, если "enter", false в противном случае
int nextAnim
- Идентификатор ресурса анимации, которая собирается играть.
Итак, например, если вы попытались сделать флешку карты, из документации:
// Create and commit a new fragment transaction that adds the fragment for the back of
// the card, uses custom animations, and is part of the fragment manager back stack.
BackOfCardFragment backFragment = new BackOfCardFragment();
getFragmentManager()
.beginTransaction()
// Replace the default fragment animations with animator resources representing
// rotations when switching to the back of the card, as well as animator
// resources representing rotations when flipping back to the front (e.g. when
// the system Back button is pressed).
.setCustomAnimations(
R.animator.card_flip_right_in, R.animator.card_flip_right_out,
R.animator.card_flip_left_in, R.animator.card_flip_left_out)
// Replace any fragments currently in the container view with a fragment
// representing the next page (indicated by the just-incremented currentPage
// variable).
.replace(R.id.container_view, backFragment)
// Add this transaction to the back stack, allowing users to press Back
// to get to the front of the card.
.addToBackStack(null)
// Commit the transaction.
.commit();
ПРИМЕЧАНИЕ: R.id.container_view в приведенном выше примере является идентификатором ViewGroup, который содержит существующий фрагмент, который вы пытаетесь заменить.
Когда выполняется вышеуказанный код, метод onCreateAnimator
будет вызван, а параметр nextAnim
будет одним из четырех идентификаторов анимации, переданных в функцию setCustomAnimations()
, то есть R.animator.card_flip_right_in, R. animator.card_flip_right_out... и т.д.
Это не кажется сразу полезным сразу, поскольку оно не дает вам ссылку на фактический объект Animator, к которому вы могли бы подключить слушателя. Но, как ни странно, вы можете просто надуть еще один Animator непосредственно из ресурса nextAnim
, а затем присоединить слушателей к тому, что, как ни странно, должно привести к потере всех перевернутых обратных вызовов в нужное время:
@Override
public Animator onCreateAnimator(int transit, boolean enter, int nextAnim) {
Animator animator = null;
// In this example, i want to add a listener when the card_flip_right_in animation
// is about to happen.
if (nextAnim == R.animator.card_flip_right_in) {
animator = AnimatorInflater.loadAnimator(getActivity(), nextAnim);
// * Sometimes onCreateAnimator will be called and nextAnim will be 0,
// causing animator to be null.
// * I wanted to add a listener when the fragment was entering -
// your use case may be different.
if (animator != null && enter) {
animator.addListener(new Animator.AnimatorListener() {
@Override
public void onAnimationStart(Animator animation) {
// Do something when the card flip animation begins
}
@Override
public void onAnimationEnd(Animator animation) {
// Do something as soon as the card flip animation is over
}
@Override
public void onAnimationCancel(Animator animation) {
}
@Override
public void onAnimationRepeat(Animator animation) {
}
});
}
}
return animator;
}
Таким образом, вы должны иметь возможность добавлять слушателей к аниматорам перехода фрагмента, как если бы вы сами их создали.