Search code examples
androidandroid-architecture-navigationshared-element-transition

how to use shared element transition in a dialog fragment


I'm using navigation component. From the RecyclerView in a fragment I want to animate the ImageView to a 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);
                });

And then in the DialogFragment I do this...

    @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;
}

This was working for fragment to fragment, but I do not want to do it using DialogFragment.


Solution

  • DialogFragment does not support shared element transitions, since Fragment shared element transitions only transition within the same window (vs dialogs, which are a separate window). Navigation does not change this fact, unfortunately.