I'm using shared element transition between two fragments. The animation works well, but there is a white flash which happens in the fragment.
In my first fragment, I set shared element transition using:
getActivity().getSupportFragmentManager()
.beginTransaction()
.addSharedElement(ivPic, ViewCompat.getTransitionName(ivPic))
.addToBackStack(null)
.replace(R.id.fragment_container, fragment)
.commit();
And in my second fragment inside onCreate():
Transition transition = TransitionInflater.from(getContext()).inflateTransition(R.transition.shared_element_transition);
setSharedElementEnterTransition(transition);
setSharedElementReturnTransition(transition);
Things I've tried:
setEnterTransition(null) in my second fragment
setCustomAnimations(0, 0) in fragment transition in first fragment
Related
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.
when you begin transaction you can add shared element transition like this
getSupportFragmentManager()
.beginTransaction()
.addSharedElement(sharedElement, transitionName)
.replace(R.id.container, newFragment)
.addToBackStack(null)
.commit();
and give same transition name in xml of other fragment
android:transitionName="transitionName"
Running a simple slide to the left animation for both entering and existing fragment produces the effect of the entering fragment slightly overlapping with the exit fragment. This leads me to think that both transition are not executed at the same time. Any clue or confirmation of this behavior?
The desired effect is to slide the fragments to the left at the same time, without overlap.
The code:
Fragment current = ...;
Fragment fragment = ...;
Transition slideIn = TransitionInflater.from(this)
.inflateTransition(R.transition.fragment_indicator_enter)
.setDuration(300)
.setInterpolator(new LinearInterpolator());
fragment.setEnterTransition(slideIn);
currentFragment.setExitTransition(TransitionInflater.from(this)
.inflateTransition(R.transition.fragment_indicator_exit)
.setDuration(300)
.setInterpolator(new LinearInterpolator()));
getSupportFragmentManager()
.beginTransaction()
.replace(R.id.fragment_container, fragment)
.addToBackStack(null)
.commit();
The only workaround by know it has been to add a setStartDelay(30) for the entering transition. But weird thing, I have different transitions for different fragments and the startDelay has to be different to produce the effect of both fragment sliding to the left at the same time.
The effect is an expected behavior of the transition, as all the views in the layout are moved at a different time to avoid moving everything as a block a create some natural sense of motion. I intentionally want this block effect, so it's solved by adding a target for the transition, where this target is the FrameLayout containing the views of the fragment.
fragment.setEnterTransition(new Slide(Gravity.RIGHT)
.addTarget(R.id.whole_content));
Did you try placing the animations directly in the transaction call?
getSupportFragmentManager()
.setCustomAnimations(R.transition.fragment_indicator_enter, R.transition.fragment_indicator_exit)
.beginTransaction()
.replace(R.id.fragment_container, fragment)
.addToBackStack(null)
.commit();
I am using native Fragments and a fragment transaction with add() and hide(). When I go back to the original fragment with the back button, the ripple animation from android:background="?attr/selectableItemBackground" resumes from when I opened the fragment originally. How do I cancel an animation when hiding a fragment in a transaction?
getFragmentManager().beginTransaction()
.hide(this)
.add(R.id.fragment_container, fragment)
.addToBackStack(null)
.commit()
Edit:
I am changing my app to use ViewPager.
I have a Fragment which contains a ListView say FragmentA, when I add another Fragment which contains another ListView say FragmentB:
getFragmentManager().beginTransaction()
.add(R.id.fragment_container, FragmentB)
.commit()
I noticed that getView() of ListView in FragmentA is still getting called even though the top fragment is now FragmentB, any reason why and how do I prevent this behavior?
You're stacking fragments on top of each other.
Instead of add use replace to replace any existing fragments in that container.
When I do transaction.setCustomAnimations(R.anim.move_left_in, R.anim.move_left_out), instead of sliding the fragment out: the window first remove the view child views and then slide. This makes for a poor user experience. What I want is for the view and all its children intact to simply slide out. I am using the same transition for activities and it works fines; no funny attenuation before sliding. How might I get the fragment transition to behave more like the activity transition?
MyFragment fragment = new MyFragment();
fragment.setArguments(bundle);
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
transaction.setCustomAnimations(R.anim.move_left_in, R.anim.move_left_out);
transaction.replace(R.id.holder, fragment, MyFragment.class.getSimpleName());
transaction.addToBackStack(null);
transaction.commit();