如何在对话框片段中使用共享元素转换

how to use shared element transition in a dialog fragment

我正在使用导航组件。从片段中的 RecyclerView,我想将 ImageView 动画化为 DialogFragment。

sdvPhoto.setOnClickListener(view -> {

                    Bundle args = new Bundle();
                    args.putString("ImageFilePath", image.getAbsolutePath());
                    args.putString("transition_name", "photo_" + getAdapterPosition());

                    ViewCompat.setTransitionName(sdvPhoto, "photo_" + getAdapterPosition());

                    DialogFragmentNavigator.Extras extras = new FragmentNavigator.Extras.Builder()
                            .addSharedElement(sdvPhoto, "photo_" + getAdapterPosition())
                            .build();

                    Navigation.findNavController(parentFragment.getView()).navigate(R.id.action_chatFragment_to_imageViewFragment, args, null, extras);
                });

然后在 DialogFragment 中我这样做...

    @Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    baseViewFragmentImageView = inflater.inflate(R.layout.fragment_image_view, container, false);
    unbinder = ButterKnife.bind(this, baseViewFragmentImageView);
    getDialog().getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));

    if (bundle != null) {
        iv.setPhotoUri(Uri.fromFile(new File(bundle.getString("ImageFilePath"))));
        iv.setTransitionName(bundle.getString("transition_name"));
        ViewCompat.setTransitionName(iv, bundle.getString("transition_name"));
    }

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        TransitionSet transitionSet = new TransitionSet();
        transitionSet.addTransition(new ChangeBounds());
        transitionSet.addTarget(iv);
        getDialog().getWindow().setSharedElementEnterTransition(transitionSet);
        this.setSharedElementReturnTransition(new ImageTransition());
    }
    return baseViewFragmentImageView;
}

这是为了片段到片段而工作,但我不想使用 DialogFragment 来完成它。

DialogFragment 不支持共享元素转换,因为 Fragment 共享元素转换仅在同一 window 内转换(相对于对话框,它们是单独的 window)。不幸的是,导航并没有改变这个事实。