【白水日记】Navigtion(二)

2021-02-26  本文已影响0人  d2665f7588cb

新建一个navigation配置文件,创建3个fragment

<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/graph_bottom_navigation_bar_activity"

    app:startDestination="@id/left">

    <fragment

        android:id="@+id/left"

        android:name="com.chip.testnav.left"

        android:label="fragment_left"

        tools:layout="@layout/fragment_left" />

    <fragment

        android:id="@+id/mid"

        android:name="com.chip.testnav.mid"

        android:label="fragment_mid"

        tools:layout="@layout/fragment_mid" />

    <fragment

        android:id="@+id/right"

        android:name="com.chip.testnav.right"

        android:label="fragment_right"

        tools:layout="@layout/fragment_right" />

</navigation>

简单写一个上fragment,下btn的界面,配置类似,至少需要写好name和navGraph

bottomNavigationView配置如下

<menu xmlns:android="http://schemas.android.com/apk/res/android">

    <item

        android:id="@+id/mid"

        android:icon="@drawable/ic_launcher_foreground"

        android:title="中"/>

    <item

        android:id="@+id/right"

        android:icon="@drawable/ic_launcher_foreground"

        android:title="右"/>

    <item

        android:id="@+id/left"

        android:icon="@drawable/ic_launcher_foreground"

        android:title="左"/>

</menu>

    将navController和View绑定,这里推荐将<fragment>和<item>标签下的id写成一样的,这样会节省代码,否则需要自己实现setOnNavigationItemSelectedListener,找到对应id通过navController执行跳转fragment的逻辑,除非有打点统计的需求,否则建议写成id相同的情况

可以看到NavigationUI类中的方法

    大部分都是将NavController和UI绑定,支持BottomNavigationVIew,ToolBar,DrawerLayout,CollapsingToolbarLayout,详情可参看

https://developer.android.google.cn/guide/navigation/navigation-ui

NavigationUI的setupWithNavController绑定的主要内容就是帮开发者实现setOnNavigationItemSelectedListener的点击行为,如果使用其他控件,需要自己实现

---------------------------------------------------------------------------

    DeepLink是Navigation封装的scheme功能,在design界面可以手动添加一个scheme,

生成代码

在deeplink标签下,可以看到约定的uri协议

能用到的一般有两个地方,推送跳转和端外跳转:

推送主要实现pendintent方法,设置好对应的graph,id即可

使用端外协议跳转,需要配置一个参数

即可实现对应跳转逻辑

上一篇 下一篇

猜你喜欢

热点阅读