如何使用新的导航图将过渡应用于片段?

问题描述 投票:1回答:1

[我使用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>
java android android-fragments android-architecture-components android-architecture-navigation
1个回答
0
投票

我不确定您的fade_infade_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" />

要将这种淡入淡出过渡效果应用于片段,您应该像已经设置的那样设置enterAnimexitAnim,但要像下面的示例一样设置popEnterAnimpopExitAnim

<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"/>
© www.soinside.com 2019 - 2024. All rights reserved.