STAMINA模式使用自定义动画打破了片段加载 [英] STAMINA mode breaks fragment loading with custom animations
问题描述
此代码可在除激活了STAMINA模式的Sony设备上的所有设备上正常工作:
This code is working as expected on all devices except Sony devices with activated STAMINA mode:
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模式下工作?
With STAMINA mode the first Fragment is loaded normally, but the next one does not show up at all (the screen remains black). If I comment out the setCustomAnimations
method, the transaction works as expected. What is going on here, how to get the transaction working with activated STAMINA mode?
R.animator.slide_in:
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>
编辑:由我自己解决,请参见下文
EDIT: solved by myself, see below
推荐答案
我使用了罗马·努里克(Roman Nurik)的方法来为片段
.如果激活了STAMINA模式,则会禁用动画,并且仅以动画的结束值调用一次 setXFraction
方法.此时 View
的宽度为0,因此永远不会将其放置在正确的位置.
I used Roman Nurik's method for animating the Fragments
. If the STAMINA mode is activated, the animations are disabled and the setXFraction
method is called only once with the end value of the animation. The width of the View
is 0 at this moment, so it's never going to be placed at the right position.
解决方案是将布局推迟到布局完成为止:
The solution is to delay the placement until the layout is finished:
public void setXFraction(float xFraction) {
this.xFraction = xFraction;
post(() -> {
final int width = getWidth();
setX((width > 0) ? (this.xFraction * width) : -9999);
});
}
这篇关于STAMINA模式使用自定义动画打破了片段加载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!