10

I just need plain slide in and slide out animation for Fragment transition, below is my code: slide_in_left.xml:

<?xml version="1.0" encoding="utf-8"?>
<set
xmlns:android="http://schemas.android.com/apk/res/android"
android:shareInterpolator="true">
<translate
    android:fromXDelta="-100%"
    android:toXDelta="0%"
    android:fromYDelta="0%"
    android:toYDelta="0%"
    android:duration="700">
</translate>
</set>

slide_out_right.xml:

<?xml version="1.0" encoding="utf-8"?>
<set
xmlns:android="http://schemas.android.com/apk/res/android"
android:shareInterpolator="false">
<translate
    android:fromXDelta="0%" android:toXDelta="100%"
    android:fromYDelta="0%" android:toYDelta="0%"
    android:duration="700">
</translate>
</set>

the code I used:

SomeFragment frag = SomeFragment.newInstance(foo);
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.setCustomAnimations(R.anim.slide_in_left, R.anim.slide_out_right);
ft.replace(R.id.the_fragment, frag);
ft.addToBackStack(null);

ft.commit();

The result looks very stranges, when the transition starts, the current fragment disappears without animation, the entering fragment comes(from left) like a scrolling paper. What's wrong with my animation xml code?

Thanks!

Programmer Bruce
  • 64,977
  • 7
  • 99
  • 97
Hao Zhe XU
  • 101
  • 1
  • 1
  • 3
  • Yes, it seems like ObjectAnimator is not in the Compatibility pack.... –  Dec 13 '11 at 14:54

4 Answers4

5

getSupportFragmentManager() means you are using the Compatibility Package for Fragments, I guess. I have the same problem, which is no animation happening at all.

See http://groups.google.com/group/android-developers/browse_thread/thread/5ef5ba1be9f40c56/a846578d91a032c0?hide_quotes=yes#msg_8ca017c473818a04

strem
  • 400
  • 1
  • 4
  • 15
3

Google has updated the compatibility library and the transitions have been fixed. You guys should update your compatiblity library from the android sdk/avd manager.

FHan
  • 528
  • 2
  • 7
  • 18
2

I found a cool post here about the Fragment Compatibility library animations issue, i took second approach

..... Call FragmentTransaction.setCustomAnimations(), referencing either animators or animations (depending on whether you're using the compatibility library or not). What is interesting is that setCustomAnimations() affects all fragment transitions added to the transaction after it is called. So you need to call setCustomAnimations() before you want it used, and you can actually setup multiple different custom animations for each part of a transaction (with a call to setCustomAnimations() before each add()/remove()/attach()/detach()/show()/hide()/replace())......

like this

FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.setCustomAnimations(R.anim.push_up_in,R.anim.push_up_out);
ft.commit();
ft = getSupportFragmentManager().beginTransaction();
ft.setCustomAnimations(R.anim.push_up_in,R.anim.push_up_out);
if (fragment1.isHidden()) {
ft.show(fragment1);
} else {
ft.hide(fragment1);

}
ft.commit();

With this i solved the problem for api level 11 hope it works!

47tucanae
  • 133
  • 2
  • 8
1

If you're using ViewPager

mPager.setOffscreenPageLimit(YourFragmentsSize); 

This line solved my problem, hope works for you too. Referanced from this topic

Community
  • 1
  • 1
yahya
  • 4,810
  • 3
  • 41
  • 58