I'm using JetPack navigation component to navigate between fragments. I've added slide animation between 2 fragments in graph xml:
<action
android:id="@+id/action_Fragment_to_DetailsFragment"
app:enterAnim="@anim/slide_left"
app:popEnterAnim="@anim/slide_right"
app:destination="@id/DetailsFragment" />
The problem is - b/c I'm using navigation component and it uses fragmentManager.replace()
, instead of fragmentManager.add()
, instead of smooth animation I see that:
the first fragment disappears
animation of 2nd fragment is being triggered and sliding new fragment to a screen
2nd fragment appears on a screen.
And b/c I have different content on both screens, it looks buggy.
I want to implement is "like in IOS" where user sees 2 layers of screens, sliding from each other. Is there a way to implement it with Navigation component which doesn't support "fragmentManager.add()
"?
I've also tried
app:enterAnim="@anim/slide_in_right"
app:exitAnim="@anim/slide_out_left"
app:popEnterAnim="@anim/slide_in_left"
app:popExitAnim="@anim/slide_out_right"
but situation is not much better.
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).
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.
Finally I found the way how to do it.
<action
android:id="@+id/action_DetailsFragment"
app:enterAnim="@anim/slide_left"
app:exitAnim="@anim/wait_anim"
app:popEnterAnim="@anim/wait_anim"
app:popExitAnim="@anim/slide_right"
app:destination="@id/detailsFragment" />
Creatie animations:
slide_left.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:shareInterpolator="false">
<translate
android:duration="300"
android:fromXDelta="100%"
android:fromYDelta="0%"
android:toXDelta="0%"
android:toYDelta="0%" />
</set>
slide_right.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:shareInterpolator="false">
<translate
android:duration="300"
android:fromXDelta="0%"
android:fromYDelta="0%"
android:toXDelta="100%"
android:toYDelta="0%" />
</set>
wait_anim.xml
<?xml version="1.0" encoding="utf-8"?>
<translate xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="300">
</translate>
To make it look better, in DetailsFragment add:
override fun onViewCreated(view: View, savedInstanceState: Bundle?) { super.onViewCreated(view, savedInstanceState) ViewCompat.setTranslationZ(getView()!!, 100f) }
You can also add sharedElementTransitions
to make your animation more unique.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With