2012-06-21 49 views
1

我想爲我的活動實現一個片段的動畫。 但是普通的startAnimation()方法不適用於fragment。如何爲Fragment設置動畫?

如何爲我的片段設置動畫? 以下是我的代碼片段:

Fragment listview = (Fragment)getSupportFragmentManager().findFragmentById(R.id.my_tab_fragment); 
final Animation a = AnimationUtils.loadAnimation(this, R.anim.translate); 

回答

2

爲動畫添加/刪除,你應該使用一個片段:

getFragmentManager().beginTransaction() 
          .setCustomAnimation(); 

你應該看看FragmentTransaction documentation

只需小心並撥打setCustomAnimation()方法,在add(fragment)replace(fragment)之前您的FragmentTransaction

+0

如何使用我擁有的代碼段來實現它? – hectichavana

1

您需要使用FragmentManager創建FragmentTransaction通常情況下,你可以有2個動畫,一個在新的幻燈片和一個滑出舊的,我會用你的兩個,像這樣:

FragmentTransaction tx = getFragmentManager().beginTransaction(); 
tx.setCustomAnimations(a, a); 
tx.replace(oldFragmentReference, newFragmentReference); 
tx.commit(); 

希望這對我有所幫助。

+0

我無法調用getFragmentManager?什麼可能是錯誤的? – hectichavana

+0

好的什麼是超級類?應該沒問題,如果你從Activity開始。如果你在HoneyComb之前,但你需要擴展FragmentActivity。 – MahdeTo