I need help regarding Fragment to fragment transitions. I am not clear on how to do it properly as there is little documentation i could find.
I have a GridView with movie posters.When one poster is clicked it will move to the next fragment.The poster is the shared element,it will resize(get bigger) and cover the screen.The other elements will fade as per the content transition.This works correctly.
Am not able to get the Re-enter transition.While pressing back button, The first element in Grid view will always be present first(it does not follow the content transition(fade)),then the other elements fade in.The shared element does not resize(get smaller) and join the other elements.It fades in with the other elements together,thus following the content transition style. Here is my Code:
NowShowingFragment.java contains the gridview with images
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setRetainInstance(true);
setHasOptionsMenu(true);
Transition shared = TransitionInflater.from(getActivity()).inflateTransition(android.R.transition.fade);
shared.setDuration(2000);
setExitTransition(shared);
}
In my OnCreateView-I am using Picasso to load images from the web.So i am first extracting the bitmap from Imageview and passing it to Activity which in turn will pass to the next fragment to display.(No idea if this is how its should be done).
mGridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
imageView = (ImageView) view.findViewById(R.id.gridImage);
Drawable drawable = imageView.getDrawable();
if (drawable instanceof BitmapDrawable) {
bmp = ((BitmapDrawable) imageView.getDrawable()).getBitmap();
} else {
return;
}
imageView.setTransitionName("Poster" + position);
mCallback.onArticleSelected(bmp, "Poster" + position, imageView);
}
});
MainActivity
@Override
public void onArticleSelected(Bitmap bmp, String name, ImageView image) {
NowShowingDetail newFragment = new NowShowingDetail();
Transition shared=TransitionInflater.from(this).inflateTransition(android.R.transition.move);
shared.setDuration(2000);
Bundle args = new Bundle();
args.putParcelable("Bitmap", bmp);
args.putString("TransitionName", name);
newFragment.setArguments(args);
newFragment.setEnterTransition(shared);
newFragment.setSharedElementEnterTransition(TransitionInflater.from(this).inflateTransition(R.transition.change_image_transform));
getSupportFragmentManager().beginTransaction().replace(R.id.now_showing_fragment, newFragment).addToBackStack(null).addSharedElement(image, image.getTransitionName()).commit();
}
In NowShowingDetail-This is the new fragment which will replace the old one.It will just display the shared element full screen.
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.testlayout, container, false);
ImageView imageView = (ImageView) rootView.findViewById(R.id.gridImage);
Bundle arguments = getArguments();
if (arguments != null) {
Bitmap bitmap = arguments.getParcelable("Bitmap");
imageView.setImageBitmap(bitmap);
imageView.setTransitionName(arguments.getString("TransitionName"));
}
return rootView;
}
change_image_transorm.xml
<transitionSet xmlns:android="http://schemas.android.com/apk/res/android">
<changeImageTransform
android:duration="2000"></changeImageTransform>
<changeBounds
android:duration="2000"></changeBounds>
<changeTransform
android:duration="2000"></changeTransform>
Thank you in advance.
EDIT: With same code I changed to activity transition method.This works Flawlessly.I think they way i call Fragment transition might be wrong.Your views appreciated.
I have noticed there is no Shared Element Exit and Re-enter transitions for Fragments.
Quoting from @George Mount post:
All of these transitions are the same in Fragment Transitions except that there are no shared element exit and shared element return transitions. Fragment Transitions work using the FragmentTransaction. You remove a Fragment, then add a Fragment, and the Transitions are activated. If you remove a Fragment, it sure is difficult to do some manipulation on it like you would in the Activity Transition. Instead, you’re going to have to do your manipulations before starting the FragmentTransaction.
from https://halfthought.wordpress.com/2014/12/08/what-are-all-these-dang-transitions/
So i think Fragment transitions are only one way.If that is the case, then behaviour is as expected.
Will Keep this as accepted until anyone else gives better explanation.