STAMINA模式使用自定义动画打破片段加载

时间:2017-01-04 18:38:52

标签: android android-fragments fragmenttransaction sony-xperia

此代码在除激活STAMINA模式的Sony设备以外的所有设备上正常运行:

int backStackCount = getSupportFragmentManager().getBackStackEntryCount();    
getFragmentManager()
                .beginTransaction()
                .setCustomAnimations(backStackCount == 0? R.animator.noanim : R.animator.slide_in,
                        R.animator.zoom_out, R.animator.zoom_in, backStackCount == 0? R.animator.noanim : R.animator.slide_out)
                .replace(R.id.container, fragment, String.valueOf(backStackCount))
                .addToBackStack(fragment.getClass().toString())
                .commit();

使用STAMINA模式时,第一个片段正常加载,但下一个片段根本不显示(屏幕保持黑色)。如果我注释掉setCustomAnimations方法,则事务按预期工作。这里发生了什么,如何让事务处理激活的STAMINA模式?

R.animator.slide_in:

<?xml version="1.0" encoding="utf-8"?>
<set>
    <objectAnimator xmlns:android="http://schemas.android.com/apk/res/android"
                    android:interpolator="@android:anim/decelerate_interpolator"
                    android:valueFrom="1.0"
                    android:valueTo="0"
                    android:propertyName="xFraction"
                    android:duration="@android:integer/config_mediumAnimTime" />
</set>

编辑:由我自己解决,见下文

1 个答案:

答案 0 :(得分:0)

我使用Roman Nurik's methodFragments制作动画。如果激活STAMINA模式,则禁用动画,并且仅使用动画的结束值调用setXFraction方法一次。此时View的宽度为0,因此它永远不会被放置在正确的位置。

解决方案是延迟放置直到布局完成:

public void setXFraction(float xFraction) {
    this.xFraction = xFraction;
    post(() -> {
        final int width = getWidth();
        setX((width > 0) ? (this.xFraction * width) : -9999);
    });
}