我使用show/hide來顯示佔據屏幕一部分的片段。出於某種原因,當片段被顯示時,動畫將播放,但是當片段被隱藏時沒有動畫,片段就會消失。我試過exit
和enter
都使用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參數確定)。
現在,我通過在第一個片段的頂部顯示第二個片段(部分覆蓋第一個片段)繞過了這個問題。在這種佈局情景下,動畫效果很好。 –