2015-11-24 67 views
4

我有一個片段,這表明進入動畫,我設置過渡由如何知道輸入過渡何時以片段結束?

this.setEnterTransition(transition); 

之後,我想顯示另一個動畫。但是我需要知道轉換動畫何時結束以開始第二個動畫。

對於活動,有一個回調如onEnterAnimationComplete(),但它在分段過渡結束時不會調用。

有沒有什麼辦法可以知道什麼時候輸入轉換結束了片段?

回答

3
transition.addListener(new Transition.TransitionListener() { 
        @Override 
        public void onTransitionStart(Transition transition) {} 

        @Override 
        public void onTransitionEnd(Transition transition) {} 

        @Override 
        public void onTransitionCancel(Transition transition) {} 

        @Override 
        public void onTransitionPause(Transition transition) {} 

        @Override 
        public void onTransitionResume(Transition transition) {} 
       }); 

       this.setEnterTransition(transition); 
1

如果你有以下設置:

FragmentA與像

private final TransitionSet transition = new TransitionSet() 
     .addTransition(new ChangeBounds()); 
//... 
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction() 
      .replace(R.id.container, fragment, fragment.getClass().getSimpleName()); 

transaction.addSharedElement(view, view.getTransitionName()); 
fragment.setSharedElementEnterTransition(transition); 
fragment.setSharedElementReturnTransition(transition); 
transaction.commit(); 

來監聽SharedElementTransition在你的第二個片段末尾SharedElementEnterTransition調用FragmentB。然後,你必須得到SharedElementEnterTransition在FragmentB的onAttach像:

@Override 
public void onAttach(Context context) { 
    super.onAttach(context); 
    TransitionSet transitionSet = (TransitionSet) getSharedElementEnterTransition(); 
    if (transitionSet != null) { 
     transitionSet.addListener(new Transition.TransitionListener() { 
      @Override 
      public void onTransitionEnd(@NonNull Transition transition) { 
       // remove listener as otherwise there are side-effects 
       transition.removeListener(this); 
       // do something here 
      } 

      @Override 
      public void onTransitionStart(@NonNull Transition transition) {} 
      @Override 
      public void onTransitionCancel(@NonNull Transition transition) {} 
      @Override 
      public void onTransitionPause(@NonNull Transition transition) {} 
      @Override 
      public void onTransitionResume(@NonNull Transition transition) {} 
     }); 
    } 
} 

正如指出的在this answer的意見有當沒有設置在onAttach聽衆()的錯誤。