My case is I'm having only 1 activity. Firstly, it displays fragment1, inside that fragment, i have a recyclerView to display a list of cardviews, inside each CardView, there is an image. When user tap on each CardView, fragment1 will be replaced by the fragment 2. Fragment2 contains a viewPager, viewpager display the same data with recyclerView in fragment1 but more specific. I want to make a shareElement animation between image1 in fragment1 to image2 in fragment2.
What I did is:
OnLickListener of recyclerView, I set transitionName for image1 by item's code
image1.setTransitionName(item.getContentID().toString());
Replace framgent and addShareElement:
fragmentTransaction
.replace(R.id.fragment, sharedElementFragment2, FragmentEnum.FEED.toString())
.addToBackStack(null)
.addSharedElement(image1,image1.getTransitionName())
.commit();
In fragment of viewPager, I update image2 with transitionName was sent by fragment 1
@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
view.findViewById(R.id.image2).setTransitionName(feed.getContentID()+"");
}
But it doesn't work. I tried to put image2 outside viewpager (directly under fragment 2 ) and it works. It seems that shareElement doesn't work when the target element inside viewPager. Any solution for this?