开发者

Android: What's wrong with my fragment transition animation?

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!


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


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.


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!


If you're using ViewPager

mPager.setOffscreenPageLimit(YourFragmentsSize); 

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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜