2

我将此代码用于片段转换:

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

但是现在当我在 Android 8.1 api 27 上运行此代码时,视图被删除并且在返回片段后不可见。

4

1 回答 1

0

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

在文档中说,只有在从布局资源文件创建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;
            }
        }
    }
}
于 2018-02-08T12:13:27.203 回答