8

我使用show/hide來顯示佔據屏幕一部分的片段。出於某種原因,當片段被顯示時,動畫將播放,但是當片段被隱藏時沒有動畫,片段就會消失。我試過exitenter都使用slide_in_left動畫,這沒有幫助。當將代碼追蹤到支持包中時,動畫就會被創建並顯示它的代碼正在執行。 (我跟蹤.hide調用)退出動畫無效; FragmentTransaction定製動畫不適用於隱藏

FragmentManager fm = _activity.getSupportFragmentManager(); 
Fragment fragment = fm.findFragmentById(R.id.my_fragment); 
FragmentTransaction ft = fm.beginTransaction(); 
ft.setCustomAnimations(R.anim.slide_in_left, R.anim.slide_out_left); 
if (fragment.isHidden()) { 
    ft.show(fragment); 
    fragment.setUserVisibleHint(true); 
} else { 
    ft.hide(fragment); 
} 
ft.commit(); 

以防萬一,這裏是爲slide_out_left動畫的XML

<set xmlns:android="http://schemas.android.com/apk/res/android"> 
    <translate android:fromXDelta="0" android:toXDelta="-50%p" 
     android:duration="@android:integer/config_mediumAnimTime"/> 
    <alpha android:fromAlpha="1.0" android:toAlpha="0.0" 
     android:duration="@android:integer/config_mediumAnimTime" /> 
</set> 

編輯: 這可能是問題的事做的事實,my_fragment與包含webview的另一個片段共享屏幕寬度。當爲my_fragment執行.show時,它變爲可見並在水平線性佈局內共享空間(兩個片段中每個片段佔用的屏幕寬度由weight參數確定)。

+0

現在,我通過在第一個片段的頂部顯示第二個片段(部分覆蓋第一個片段)繞過了這個問題。在這種佈局情景下,動畫效果很好。 –

回答

3

對我個人而言,這是一個zPosition for Fragment動畫的bug。

你看到的是新片段「出現」,這是因爲新片段附加在當前片段下面。

因此,當試圖對現有片段的「頂部」進行動畫設置時,它看起來好像什麼也沒有發生,然後新的只出現。

我已經嘗試了許多解決方法,玩zPosition(隻影響窗口不佈局,所以對碎片沒有影響),onAnimationCreate()將根佈局放在前面,甚至在動畫開始和停止時......似乎什麼都不做。

所以在這個時候,如果沒有爲片段轉換寫一個完整的自定義動畫,他們目前有點bug。

你可能要玩。 .add(Fragment)等待它被添加,然後調用.remove(Fragment)刪除舊的片段,這樣新的片段物理放置在現有片段的頂部。

+0

聽起來似乎合理,但它不是很清楚爲什麼在一個片段位於另一個片段之上的佈局中,動畫工作正常。顯示碎片的代碼保持不變,所以這個zPosition問題應該仍然有效? 我提到的解決方法竟然是首選方式,因爲WebView片段不需要重新呈現其內容。 –

+0

從動畫的進一步體驗中,我發現越來越多的這些類型的錯誤是由於zPosition引起的,所以儘管我的應用程序不再包含這個確切的功能/錯誤,我接受了您的答案。 –

+0

謝謝@LeoK現在我正在擺脫碎片,贊成View + Controller模式,就像Squares Flow庫一樣。非常靈活。 –

-4

我能得到出口用動畫代替。新增

ft.replace(R.id.container, new MyFragment()); 
+0

replace()可能是一個壞主意,因爲如果每次在片段之間切換時創建新的Fragment,則可能會泄漏視圖。視圖可以參考上下文(活動)並且不會收集垃圾。 –

17

嘗試.replace使用 setCustomAnimations工作(INT輸入,詮釋出,詮釋popEnter,INT popExit) 代替 setCustomAnimations(INT輸入,詮釋出) 當你將調用popupbackstack退出動畫不會被稱爲

+0

謝謝,它的工作原理 – OWADVL

+0

這與彈出背景的動畫無關。他詢問關於出口的分層並輸入片段的動畫 –

1

我覺得有這樣出現了同樣的情況:

我有N片段,我想要自定義動畫,但我不想重新加載片段,所以做一個addremovereplace是不是一個解決方案,並與show/hide我無法同步動畫在兩個片段之間。

要解決它,我必須使用每個片段的FrameLayout。

這裏的代碼。

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 

    <FrameLayout 
     android:layout_above="@+id/main_bottom_navigation" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent"> 

     <FrameLayout 
      android:id="@+id/library" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent"/> 

     <FrameLayout 
      android:id="@+id/search" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent"/> 

     <FrameLayout 
      android:id="@+id/discover" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent"/> 

    </FrameLayout> 

    <com.roughike.bottombar.BottomBar 
     android:id="@+id/main_bottom_navigation" 
     android:layout_width="match_parent" 
     android:layout_height="50dp" 
     android:layout_alignParentBottom="true" 
     app:bb_tabXmlResource="@xml/main_bb_tabs" 
     app:bb_inActiveTabColor="@color/taupegray" 
     app:bb_activeTabColor="@color/green_apple" 
     app:bb_showShadow="true"/> 

</RelativeLayout> 

然後控制哪些片段顯示:

private void showFragment(BaseFragment fragment) { 
    Animation animation = AnimationUtils.loadAnimation(this, 
       R.anim.slide_in_from_right); 

    if (fragment instanceof DiscoverFragment) { 
     mDiscoverContainer.bringToFront(); 
     mDiscoverContainer.startAnimation(animation); 

     return; 
    } 

    if (fragment instanceof LibraryFragment) { 
     mLibraryContainer.bringToFront(); 
     mLibraryContainer.startAnimation(animation); 

     return; 
    } 

    if (fragment instanceof SearchFragment) { 
     mSearchContainer.bringToFront(); 
     mSearchContainer.startAnimation(AnimationUtils.loadAnimation(animation); 

     return; 
    } 
} 

的關鍵是在bringToFront

我希望它一直是有幫助!

+0

「bringToFront」方法在哪裏? –

+0

該方法已在所有視圖上實現,只需調用它即可。 –