如何使用新的导航图将过渡应用于片段?
How to apply transition to fragments using the new Navigation Graph?
我使用 XML
为淡入和淡出创建了一个动画,并使用用于进入和退出的导航下拉列表将其附加到片段,但片段没有动画。
<?xml version="1.0" encoding="utf-8"?>
<navigation xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/navigation_graph"
app:startDestination="@id/mainFragment">
<fragment
android:id="@+id/mainFragment"
android:name="com.cheapapps.randomquotesmachine.MainFragment"
android:label="fragment_main"
tools:layout="@layout/fragment_main">
<action
android:id="@+id/action_mainFragment_to_quoteDetailFragment"
app:destination="@id/quoteDetailFragment"
app:enterAnim="@anim/fade_in"
app:exitAnim="@anim/fade_out" />
</fragment>
<fragment
android:id="@+id/quoteDetailFragment"
android:name="com.cheapapps.randomquotesmachine.QuoteDetailFragment"
android:label="fragment_quote_detail"
tools:layout="@layout/fragment_quote_detail">
<argument
android:name="position"
android:defaultValue="0"
app:argType="integer" />
</fragment>
</navigation>
我不确定您的 fade_in
和 fade_out
文件是什么样的,所以我将在下面为您提供一个示例:
<?xml version="1.0" encoding="utf-8"?>
<alpha xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="@android:anim/accelerate_interpolator"
android:fromAlpha="0.0" android:toAlpha="1.0"
android:duration="500" />
并且:
<?xml version="1.0" encoding="utf-8"?>
<alpha xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="@android:anim/accelerate_interpolator"
android:fromAlpha="1.0" android:toAlpha="0.0"
android:fillAfter="true"
android:duration="500" />
要将此淡入淡出过渡应用到您的片段,您应该设置 enterAnim
和 exitAnim
,就像您已经设置的那样,但要与 popEnterAnim
和 popExitAnim
一起设置,如下所示示例:
<action android:id="@+id/action_mainFragment_to_quoteDetailFragment"
app:destination="@id/quoteDetailFragment"
app:popEnterAnim="@anim/fade_in"
app:popExitAnim="@anim/fade_out"
app:enterAnim="@anim/fade_in"
app:exitAnim="@anim/fade_out"/>
我使用 XML
为淡入和淡出创建了一个动画,并使用用于进入和退出的导航下拉列表将其附加到片段,但片段没有动画。
<?xml version="1.0" encoding="utf-8"?>
<navigation xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/navigation_graph"
app:startDestination="@id/mainFragment">
<fragment
android:id="@+id/mainFragment"
android:name="com.cheapapps.randomquotesmachine.MainFragment"
android:label="fragment_main"
tools:layout="@layout/fragment_main">
<action
android:id="@+id/action_mainFragment_to_quoteDetailFragment"
app:destination="@id/quoteDetailFragment"
app:enterAnim="@anim/fade_in"
app:exitAnim="@anim/fade_out" />
</fragment>
<fragment
android:id="@+id/quoteDetailFragment"
android:name="com.cheapapps.randomquotesmachine.QuoteDetailFragment"
android:label="fragment_quote_detail"
tools:layout="@layout/fragment_quote_detail">
<argument
android:name="position"
android:defaultValue="0"
app:argType="integer" />
</fragment>
</navigation>
我不确定您的 fade_in
和 fade_out
文件是什么样的,所以我将在下面为您提供一个示例:
<?xml version="1.0" encoding="utf-8"?>
<alpha xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="@android:anim/accelerate_interpolator"
android:fromAlpha="0.0" android:toAlpha="1.0"
android:duration="500" />
并且:
<?xml version="1.0" encoding="utf-8"?>
<alpha xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="@android:anim/accelerate_interpolator"
android:fromAlpha="1.0" android:toAlpha="0.0"
android:fillAfter="true"
android:duration="500" />
要将此淡入淡出过渡应用到您的片段,您应该设置 enterAnim
和 exitAnim
,就像您已经设置的那样,但要与 popEnterAnim
和 popExitAnim
一起设置,如下所示示例:
<action android:id="@+id/action_mainFragment_to_quoteDetailFragment"
app:destination="@id/quoteDetailFragment"
app:popEnterAnim="@anim/fade_in"
app:popExitAnim="@anim/fade_out"
app:enterAnim="@anim/fade_in"
app:exitAnim="@anim/fade_out"/>