Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to animate Android Navigation Architecture fragment as sliding over old fragment?

Tags:

In example navigation action defined in navigation graph:

<action     android:id="@+id/action_fragment1_to_fragment2"     app:destination="@id/fragment2"     app:enterAnim="@anim/right_slide_in"     app:popExitAnim="@anim/left_slide_out"/> 

When Fragment2 opens and starts sliding into view from the right, Fragment1 disappears instantly (sadly). When Fragment2 is closed and starts sliding to the right, Fragment1 is nicely visible under it, giving a nice stack pop effect (comparable to iOS).

How can I keep Fragment1 visible while Fragment2 slides into view?

like image 917
xinaiz Avatar asked May 14 '19 15:05

xinaiz


People also ask

How do you animate fragment transitions?

At a high level, here's how to make a fragment transition with shared elements: Assign a unique transition name to each shared element view. Add shared element views and transition names to the FragmentTransaction . Set a shared element transition animation.

How do you animate a fragment?

To animate the transition between fragments, or to animate the process of showing or hiding a fragment you use the Fragment Manager to create a Fragment Transaction . Within each Fragment Transaction you can specify in and out animations that will be used for show and hide respectively (or both when replace is used).


1 Answers

EDIT: This is not the most elegant solution, it is actually a trick but it seems to be the best way to solve this situation until the NavigationComponent will include a better approach.

So, we can increase translationZ (starting with API 21) in Fragement2's onViewCreated method to make it appear above Fragment1.

Example:

@Override public void onViewCreated(View view, Bundle savedInstanceState) {     super.onViewCreated(view, savedInstanceState);     ViewCompat.setTranslationZ(getView(), 100f); } 

As very nice @xinaiz suggested, instead of 100f or any other random value, we can use getBackstackSize() to assign to the fragment a higher elevation than the previous one.

The solution was proposed by @JFrite at this thread
FragmentTransaction animation to slide in over top
More details can be found there.

like image 65
sergapo Avatar answered Oct 18 '22 20:10

sergapo