我正在使用导航组件。从片段的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共享元素转换仅在同一窗口(与对话框,是一个单独的窗口)内进行转换。不幸的是,导航并不能改变这一事实。