簡體   English   中英

如何修復jetpack導航彈出行為不正確的動畫?

[英]How to fix jetpack navigation pop behaviour incorrect animation?

當使用jetpack導航主機組件時,我遇到了導航“Pop Behavior”的問題,使用了與我想要的動畫不同的動畫,我不知道如何強制使用不同的動畫。

在此輸入圖像描述 當在屏幕之間流動時,我試圖讓它們“從右向左流動”,類似於iOS的導航。 然而,當我添加pop行為以便Fragment 3直接返回到Fragment 1 ,從Fragment 2Fragment 3 Fragment 2向右移動而不是向左移動。

我在這個存儲庫中重現了這個問題: https//github.com/AtomicSimon/Android-Replication-Screen-Flow

這是我正在使用的簡單屏幕流程:

這是導航圖XML:


    <fragment
        android:id="@+id/fragment_1"
        android:label="fragment_fragment_1"
        android:name="com.example.android_replication_screen_flow.fragment_1"
        tools:layout="@layout/fragment_fragment_1">
        <action
            android:id="@+id/action_fragment_1_to_fragment_2"
            app:destination="@id/fragment_2"
            app:enterAnim="@anim/slide_in_from_the_right"
            app:exitAnim="@anim/slide_out_to_the_left"
            app:popEnterAnim="@anim/slide_in_from_the_left"
            app:popExitAnim="@anim/slide_out_to_the_right"/>
    </fragment>
    <fragment
        android:id="@+id/fragment_2"
        android:label="fragment_fragment_2"
        android:name="com.example.android_replication_screen_flow.fragment_2"
        tools:layout="@layout/fragment_fragment_2">
        <action
            android:id="@+id/action_fragment_2_to_fragment_3"
            app:destination="@id/fragment_3"
            app:enterAnim="@anim/slide_in_from_the_right"
            app:exitAnim="@anim/slide_out_to_the_left"
            app:popEnterAnim="@anim/slide_in_from_the_left"
            app:popExitAnim="@anim/slide_out_to_the_right"
            app:popUpTo="@+id/fragment_2"
            app:popUpToInclusive="true"/>
    </fragment>
    <fragment
        android:id="@+id/fragment_3"
        android:label="fragment_fragment_3"
        android:name="com.example.android_replication_screen_flow.fragment_3"
        tools:layout="@layout/fragment_fragment_3"/>

我想Fragment 2Fragment 2Fragment 3時離開屏幕向左,但我無法正確。

我有同樣的問題。

我做的是

  • 不要使用popUpTo
  • 使用popUpToInclusive進行操作而不是僅使用后台堆棧。

我個人認為這是導航中的一些動畫錯誤。

為了完成這項工作,您必須執行以下操作:

i。)從動作“@ + id / action_fragment_2_to_fragment_3”中刪除“popUpTo”

ii。)向fragment_3添加一個新動作,該動作導航回到fragment_1並為您清除背斜

<fragment
    android:id="@+id/fragment_3"
    android:label="fragment_fragment_3"
    android:name="com.example.android_replication_screen_flow.fragment_3"
    tools:layout="@layout/fragment_fragment_3">

    <action
        android:id="@+id/backToFragment_1_action"
        app:destination="@id/fragment_1"
        app:enterAnim="@anim/enter_from_left"
        app:exitAnim="@anim/exit_to_right"
        app:popUpToInclusive="true"
        app:popUpTo="@id/fragment_1" />

</fragment>

iii。)為了在用戶單擊后退按鈕時具有相同的行為,您應該在Fragment_3的onCreate()中添加OnBackPressedCallback並調用“backToFragment_1_action”。

@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    // This callback will only be called when MyFragment is at least Started.
    requireActivity().getOnBackPressedDispatcher().addCallback(this, new OnBackPressedCallback(true) {
        @Override
        public void handleOnBackPressed() {
            NavHostFragment.findNavController(Fragment_3.this).navigate(R.id. backToFragment_1_action);
        }
    });
}

我有同樣的問題,我認為問題在於如何處理:

我的導航是:A < - > B - > C.

所以當從A到B時,backstack會添加B,當轉到C時,應該彈出所有內容。 而這正是問題所在。

A - > B應該從右向左滑動。

B - >后面(彈出動作)應該從左向右滑動相反的方向。

但正因為如此,我認為XML中的操作適用於視圖,而不僅僅是一個過渡 因此,每當B彈出時,它就會向右移動。

B - > C表示,C從右側滑入,但B記得它必須向右滑動才能完成之前定義的動作。

老實說知道這是有道理的:前面或后面的正常導航按預期動畫。 當你從堆棧彈出一切看起來有點像洗牌。 你拿走一個,放入另一個。

如果你經常這樣做就行不通,因為你有一些瘋狂的工作流程(就像我有的,比ABC更多的地方),如果你只想使用后退按鈕和導航它就不起作用堆棧而不是每次單獨定義后退導航操作。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM