如何将Android导航架构片段动画化为在旧片段上滑动?

xin*_*aiz 17 java animation android android-fragments android-architecture-navigation

在导航图中定义的示例导航操作中:

<action
    android:id="@+id/action_fragment1_to_fragment2"
    app:destination="@id/fragment2"
    app:enterAnim="@anim/right_slide_in"
    app:popExitAnim="@anim/left_slide_out"/>
Run Code Online (Sandbox Code Playgroud)

Fragment2打开并开始从右侧滑入视图时,其Fragment1立即消失(可悲)。当Fragment2close关闭并开始向右滑动时,Fragment1在它下面可以很好地看到,从而提供很好的堆栈弹出效果(与iOS相比)。

滑入视图Fragment1时如何保持可见Fragment2

ser*_*apo 9

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);
}
Run Code Online (Sandbox Code Playgroud)

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.

  • 可悲的是,这就是我实际上所做的-它可以按需工作。我实际上做了:`ViewCompat.setTranslationZ(getView(),getBackstackSize());`在我的基本片段中,所以每个片段自动设置的是自己的translationZ,它比以前的片段要高(看起来不错!)。我知道这是“可怕的”,但我现在无法想到其他解决方案。如果有人认为解决方案不好,我将开始悬赏。 (4认同)

dha*_*sky 5

看来你错误地使用了popExitAnim代替exitAnim

一般规则是:

  • 当您打开()新屏幕时,enterAnimexitAnim发生

  • 当你弹出屏幕,popEnterAnimpopExitAnim发生

因此,您应该为每个过渡指定所有 4 个动画。
例如,我使用这些:

<action
    android:id="@+id/mainToSearch"
    app:destination="@id/searchFragment"
    app:enterAnim="@anim/slide_in_right"
    app:exitAnim="@anim/slide_out_left"
    app:popEnterAnim="@anim/slide_in_left"
    app:popExitAnim="@anim/slide_out_right" />
Run Code Online (Sandbox Code Playgroud)


小智 5

为了防止在新片段的滑动动画过程中旧片段消失,首先制作一个空动画,只包含滑动动画的持续时间。我会称之为@anim/stationary

<?xml version="1.0" encoding="utf-8"?>
<translate xmlns:android="http://schemas.android.com/apk/res/android"
           android:duration="@slidingAnimationDuration" />
Run Code Online (Sandbox Code Playgroud)

然后在导航图中,将动作的退出动画设置为新创建的空动画:

    <fragment android:id="@+id/oldFragment"
              android:name="OldFragment">
        <action android:id="@+id/action_oldFragment_to_newFragment"
                app:destination="@id/newFragment"
                app:enterAnim="@anim/sliding"
                app:exitAnim="@anim/stationary"
    </fragment>
Run Code Online (Sandbox Code Playgroud)

退出动画应用于旧片段,因此旧片段在动画的整个持续时间内都是可见的。

我对旧片段消失的原因的猜测是,如果您不指定退出动画,则默认情况下,旧片段将在进入动画开始时立即删除。