1

爲進入和現有片段運行左側動畫的簡單幻燈片會產生輸入片段與退出片段略有重疊的效果。這導致我認爲兩個轉換不是同時執行的。任何線索或確認這種行爲?片段進入和退出轉換不會同時執行

希望的效果是將碎片同時向左滑動,不會重疊。

代碼:

Fragment current = ...; 
Fragment fragment = ...; 
Transition slideIn = TransitionInflater.from(this) 
    .inflateTransition(R.transition.fragment_indicator_enter) 
    .setDuration(300) 
    .setInterpolator(new LinearInterpolator()); 
fragment.setEnterTransition(slideIn); 

currentFragment.setExitTransition(TransitionInflater.from(this) 
    .inflateTransition(R.transition.fragment_indicator_exit) 
    .setDuration(300) 
    .setInterpolator(new LinearInterpolator())); 

getSupportFragmentManager() 
    .beginTransaction() 
    .replace(R.id.fragment_container, fragment) 
    .addToBackStack(null) 
    .commit(); 

唯一的解決方法通過知道它已添加setStartDelay(30)的進入過渡。但奇怪的是,對於不同的片段,我有不同的轉換,startDelay必須有所不同,才能同時產生片段向左滑動的效果。

回答

0

效果是過渡的一個預期的行爲,如在佈局所有視圖都在不同的時間移動,以避免移動一切作爲一個塊一個產生運動的一些自然感。我故意想要這個塊效果,所以它通過添加一個轉換目標來解決,這個目標是包含片段視圖的FrameLayout。

fragment.setEnterTransition(new Slide(Gravity.RIGHT) 
        .addTarget(R.id.whole_content)); 
-1

您是否嘗試將動畫直接放入交易通話中?

getSupportFragmentManager() 
.setCustomAnimations(R.transition.fragment_indicator_enter, R.transition.fragment_indicator_exit) 
.beginTransaction() 
.replace(R.id.fragment_container, fragment) 
.addToBackStack(null) 
.commit(); 
+0

感謝您的建議,但這在我的情況下無效。我想使用內容轉換(棒棒糖api),這是不完全相同的事情。我使用自定義轉換將不同的效果應用於不同的目標視圖。 – juanmeanwhile