其他分享
首页 > 其他分享> > 【Android,Jetpack】简单接入Navigation导航

【Android,Jetpack】简单接入Navigation导航

作者:互联网

Navigation导航

效果

在这里插入图片描述

配置

依赖

implementation 'androidx.navigation:navigation-fragment-ktx:2.3.5'
implementation 'androidx.navigation:navigation-ui-ktx:2.3.5'

Crazy Coding

navigation.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"
    app:startDestination="@id/nav_login">

    <fragment
        android:id="@+id/nav_login"
        android:name="com.xx.xxx.ui.fragment.LoginFragment"
        tools:layout="@layout/fragment_login"
        android:label="登录界面">
        <action
            android:id="@+id/action_nav_login_to_nav_select"
            app:destination="@id/nav_select"
            app:enterAnim="@anim/slide_in_right"
            app:exitAnim="@anim/slide_out_left"
            app:popEnterAnim="@android:anim/slide_in_left"
            app:popExitAnim="@android:anim/slide_out_right"/>
    </fragment>

    <fragment
        android:id="@+id/nav_select"
        android:name="com.xx.xxx.ui.fragment.SelectFragment"
        tools:layout="@layout/fragment_select"
        android:label="选择界面">
        <action
            android:id="@+id/action_nav_select_to_nav_login"
            app:destination="@id/nav_login"
            app:enterAnim="@android:anim/slide_in_left"
            app:exitAnim="@android:anim/slide_out_right"
            app:popEnterAnim="@anim/slide_in_right"
            app:popExitAnim="@anim/slide_out_left" />
    </fragment>
</navigation>

slide_in_right.xml

<set xmlns:android="http://schemas.android.com/apk/res/android">
    <translate android:fromXDelta="50%p" android:toXDelta="0"
        android:duration="@android:integer/config_mediumAnimTime"/>
    <alpha android:fromAlpha="0.0" android:toAlpha="1.0"
        android:duration="@android:integer/config_mediumAnimTime" />
</set>

slide_out_left.xml

<set xmlns:android="http://schemas.android.com/apk/res/android">
    <translate android:fromXDelta="0" android:toXDelta="-50%p"
        android:duration="@android:integer/config_mediumAnimTime"/>
    <alpha android:fromAlpha="1.0" android:toAlpha="0.0"
        android:duration="@android:integer/config_mediumAnimTime" />
</set>

activity_main.xml

<FrameLayout
        ...>

        <fragment
            android:id="@+id/main_fragment_container"
            android:name="androidx.navigation.fragment.NavHostFragment"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:defaultNavHost="true"
            app:navGraph="@navigation/navigation" />
    </FrameLayout>

How To Use

findNavController().navigate(R.id.action_nav_login_to_nav_select)

标签:xml,androidx,implementation,Jetpack,nav,navigation,2.3,Android,Navigation
来源: https://blog.csdn.net/weixin_42473228/article/details/120484290