Android 8.1 Fragment setExitTransition错误

时间:2017-12-15 18:54:00

标签: android android-fragments android-transitions android-8.1-oreo

我正在使用此代码进行片段转换:

Transition transition = new Fade();
transition.addTarget(recyclerView);
setExitTransition(transition);

但现在当我在Android 8.1 api 27上运行此代码时,视图将被删除,并且在返回片段后无法显示。

1 个答案:

答案 0 :(得分:0)

在27 API中更改了方法TransitionUtils.createViewBitmap,现在它还在View中添加ViewGroupOverlay,这需要从先前View的层次结构Scene中删除视图。如何导致副作用,因为此方法旨在绕过删除View

在文档中说,只有在从布局资源文件创建View的情况下,才能从父项中删除Scene

更多细节Visibility.onDisappear

尝试使用它:

@TargetApi(19)
public class FadeSafeOreo extends Fade {

    public FadeSafeOreo() {
        super();
    }

    public FadeSafeOreo(int fadingMode) {
        super(fadingMode);
    }

    @TargetApi(21)
    public FadeSafeOreo(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    public Animator onDisappear(ViewGroup sceneRoot, TransitionValues startValues, int startVisibility, TransitionValues endValues, int endVisibility) {
        RemovedView removedView = null;
        boolean existStart = startValues != null && startValues.view != null;
        boolean existEnd = endValues != null && endValues.view != null;
        if (Build.VERSION.SDK_INT >= 27 && !canRemoveViews() && existStart && !existEnd) {
            removedView = new RemovedView(startValues.view);
        }
        Animator animator = super.onDisappear(sceneRoot, startValues, startVisibility, endValues, endVisibility);
        if (removedView != null) removedView.append();
        return animator;
    }

    private static class RemovedView {
        private View view;
        private ViewGroup parent;
        private int position = -1;

        private RemovedView(View view) {
            this.view = view;

            if (view != null && view.getParent() instanceof ViewGroup) {
                parent = (ViewGroup) view.getParent();
                for (int i = 0, count = parent.getChildCount(); i < count; i++) {
                    if (parent.getChildAt(i) == view) {
                        position = i;
                        return;
                    }
                }
            }
        }

        private void append() {
            if (view != null) {
                if (parent != null) {
                    if (view.getParent() != parent && position >= 0) {
                        parent.addView(view, position);
                    }
                    parent = null;
                }
                view = null;
            }
        }
    }
}