I create application where in main fragment there is viewPager with fragments with recyclerView. Each item of recyclerView has image, that need to be made shared to detail fragment with animation. The problem is that it doesn't work. I tried to make it without viewPager, only use one fragment with recyclerView and it works good. What the difference in realization shared elements transition in viewPager and without this one?
In adapeter I set transitionName
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
holder.movieImage.setTransitionName("transition" + position);
}
In Activity I show detail fragment:
public void showFragmentWithTransition(Fragment current, Fragment newFragment, String tag, View sharedView, String sharedElementName) {
FragmentManager fragmentManager = getSupportFragmentManager();
// check if the fragment is in back stack
boolean fragmentPopped = fragmentManager.popBackStackImmediate(tag, 0);
if (fragmentPopped) {
// fragment is pop from backStack
} else {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
current.setSharedElementReturnTransition(TransitionInflater.from(this).inflateTransition(R.transition.default_transition));
current.setExitTransition(TransitionInflater.from(this).inflateTransition(android.R.transition.no_transition));
newFragment.setSharedElementEnterTransition(TransitionInflater.from(this).inflateTransition(R.transition.default_transition));
newFragment.setEnterTransition(TransitionInflater.from(this).inflateTransition(android.R.transition.no_transition));
}
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.fragment_container, newFragment, tag);
fragmentTransaction.addToBackStack(tag);
fragmentTransaction.addSharedElement(sharedView, sharedElementName);
fragmentTransaction.commit();
}
}
In detail fragment I obtain arguments like this:
@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
Bundle arguments = getArguments();
if (arguments != null) {
String transitionName = arguments.getString("transitionName");
Movie movie = (Movie) arguments.getSerializable("movie");
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
movieImage.setTransitionName(transitionName);
}
if (movie != null) {
if (!TextUtils.isEmpty(movie.getImage()))
Picasso.with(getActivity()).load("https://i.ytimg.com/vi/qh7LLydY8eo/maxresdefault.jpg").into(movieImage);
else
movieImage.setImageDrawable(null);
}
}
}