共享元素转换在 return 上留下白色图像(使用导航)

SharedElement transition leaving white image on return (using Navigation)

我有一个包含图像的 RecyclerView(包含在 ViewPager 中),当用户单击它时,我想全屏显示它们,并使用共享元素转换。我已经成功地在我的两个片段之间实现了共享元素转换,但是当我点击 return 时,我点击的 ImageView 是白色的。

在我的 onBindViewHolder 方法中我有:

 holder.picturePost.setOnClickListener(v -> {
                NavDirections action = ProfileFragmentDirections.viewPostDetails(post);
                v.setTransitionName(post);
                FragmentNavigator.Extras extras = new FragmentNavigator.Extras.Builder().addSharedElement(v, post).build();
                Navigation.findNavController(v).navigate(action, extras);
            });

我的目标片段是 OpenPostFragment:

public class OpenPostFragment extends Fragment {

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        postponeEnterTransition();
        setSharedElementEnterTransition(TransitionInflater.from(getContext()).inflateTransition(android.R.transition.move));
        setSharedElementReturnTransition(TransitionInflater.from(getContext()).inflateTransition(android.R.transition.move));
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View root = inflater.inflate(R.layout.post_card_fragment, container, false);
        Bundle args = getArguments();
        if (args != null) {
            ImageView picturePost = root.findViewById(R.id.picturePost);
            String imagePath = args.getString("imagePath");
            picturePost.setTransitionName(imagePath);
            Glide.with(picturePost)
                    .load(imagePath)
                    .dontTransform()
                    .listener(new RequestListener<Drawable>() {
                        @Override
                        public boolean onLoadFailed(@Nullable GlideException e, Object model, Target<Drawable> target, boolean isFirstResource) {
                            startPostponedEnterTransition();
                            return false;
                        }

                        @Override
                        public boolean onResourceReady(Drawable resource, Object model, Target<Drawable> target, DataSource dataSource, boolean isFirstResource) {
                            startPostponedEnterTransition();
                            return false;
                        }
                    })
                    .into(picturePost);
        }
        return root;
    }
}

我终于解决了我的问题。我的 RecyclerView 在 ViewPager 中的事实造成了麻烦。为了解决我的问题,我必须将以下代码添加到我初始化 ViewPager 的片段中:

postponeEnterTransition();
ViewTreeObserver.OnPreDrawListener onPreDrawListener = new ViewTreeObserver.OnPreDrawListener() {
     @Override
     public boolean onPreDraw() {
           mPager.getViewTreeObserver().removeOnPreDrawListener(this);
           startPostponedEnterTransition();
           return true;
     }
};
mPager.getViewTreeObserver().addOnPreDrawListener(onPreDrawListener);