Jetpack第七篇:Navigation 组件

作者: AxeChen | 来源:发表于2022-07-13 23:18 被阅读0次

Navigation是一个路由组件,是一个优秀的Fragment管理工具,同样也可以管理Activity。开发者可以将重点放在业务开发上,避免处理太多了Fragment管理代码和调用代码,从而加速业务开发效率。

1、Navigation的组成

导航图

这个是在Android的资源文件路径下的navigation的文件夹下的资源文件,在这个资源文件中能够配置Fragment、Activity已经跳转动作action等等。

NavHost

这个是显示页面的空白容器,默认为NavHostFragment,可以理解为所有的Fragment都依靠它来显示。

NavController

NavHost中的管理对象,可以通过Controller对象去跳转Fragment和回退栈。也可以用于绑定NavigationBar等操作。

资源

这里说的就是Fragment、Activity等布局资源了,如果和BottomNavigationView绑定还需要menu资源等等。

2、实现一个简单的Navigation

实现一个非常简单的Navigation用法Demo,这里先展示一个Fragment,然后在FragmentA中有一个按钮,点击进入FragmentB。我们根据前面所说的几个部分一个个添加进入

1、导入依赖

    def nav_version = "2.3.0"
    implementation "androidx.navigation:navigation-fragment:$nav_version"
    implementation "androidx.navigation:navigation-ui:$nav_version"

2、添加导航图
在res下新建navigation资源文件,并且新建一个nav.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"
    app:startDestination="@id/fmt_a"
    android:id="@+id/nav">

    <fragment
        android:id="@+id/fmt_a"
        android:name="com.mg.axechen.libnavigation.fragment.FragmentA"
        app:layout="@layout/fragment_a"
        android:label="FramentA" >

        <action android:id="@+id/action_fmta_to_fmtb"
            app:destination="@id/fmt_b"
            app:enterAnim="@anim/nav_default_enter_anim"
            app:exitAnim="@anim/nav_default_exit_anim"/>

    </fragment>

    <fragment
        android:id="@+id/fmt_b"
        android:name="com.mg.axechen.libnavigation.fragment.FragmentB"
        app:layout="@layout/fragment_b"
        android:label="FragmentB" />


</navigation>

注意:这里的navigation 一定要添加一个初始的Fragment,app:startDestination="@id/fmt_a" 否则会出现异常:

     Caused by: android.view.InflateException: Binary XML file line #14 in com.mg.axechen.libnavigation:layout/activity_test: Error inflating class fragment
     Caused by: java.lang.IllegalStateException: no start destination defined via app:startDestination for com.mg.axechen.libnavigation:id/nav

这里对里面的这些配置做下说明:

    <fragment
        android:id="@+id/fmt_a"   // 指定Fragment的Id
        android:name="com.mg.axechen.libnavigation.fragment.FragmentA" // 指定是哪个Fragment
        app:layout="@layout/fragment_a" // 指定Fragment的资源文件
        android:label="FramentA" >

        <action android:id="@+id/action_fmta_to_fmtb" // action的id
            app:destination="@id/fmt_b" // 目标Fragment的id,这里指跳转
            app:enterAnim="@anim/nav_default_enter_anim"  // 默认进入动画
            app:exitAnim="@anim/nav_default_exit_anim"/> // 默认退出动画

这里面也可以配置Activity用法和Fragment一致。

3、添加NavHost

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_height="match_parent">

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

</FrameLayout>

这里有几个参数:

        android:name="androidx.navigation.fragment.NavHostFragment"
        app:navGraph="@navigation/nav"
        app:defaultNavHost="true"

第一个是name,目前默认要指定NavHostFragment。
第二个是指定导航图,这里指定navigation下的nav资源文件。
第三个参数会导致Fragment的回退栈发生变化。这里后面细说。

4、添加NavController

public class TestActivity extends AppCompatActivity {

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_test);
        initNavigation();
    }

    private void initNavigation() {
        NavController navController = Navigation.findNavController(this, R.id.fmt_test);
    }
}

这样一来,当启动Test Activity时,TestActivity就会通过NavController显示FragmentA了。
FragmentA也可以通过NavControl去跳转到FragmentB。

public class FragmentA extends Fragment {

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_a,container,false);

        Button test = view.findViewById(R.id.test);
        test.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Navigation.findNavController(view).navigate(R.id.action_fmta_to_fmtb);
            }
        });

        return view;
    }
}

3、和BottomNavigationView联用

这也是一个常用的用法,BottomNavigationView的使用方法这里就不多做说明了,如果需要和BottonNavigationView联用,还需要NavController做好绑定关系。
创建BottomNavigationView的menu时,需要将menu的id和在导航图配置的id一致:
比如:

<menu xmlns:android="http://schemas.android.com/apk/res/android">
    <!-- item的id必须和navigation中的资源id一致 -->
    <item android:id="@+id/fmt_a"
        android:icon="@mipmap/index"
        android:title="首页"></item>

    <item android:id="@+id/fmt_b"
        android:icon="@mipmap/cate"
        android:title="列表"></item>

    <item android:id="@+id/fmt_c"
        android:icon="@mipmap/cart"
        android:title="购物车"></item>

    <item android:id="@+id/fmt_d"
        android:icon="@mipmap/me"
        android:title="我的"></item>
</menu>

导航图:

    <!-- 资源 -->
    <fragment
        android:id="@+id/fmt_a"
        android:name="com.mg.axechen.libnavigation.fragment.FragmentA"
        tools:layout="@layout/fragment_a"
        android:label="FramentA" >

        <!--  action_now_to_target -->
        <action android:id="@+id/action_fmta_to_fmtb"
            app:destination="@id/fmt_b"
            app:enterAnim="@anim/nav_default_enter_anim"
            app:exitAnim="@anim/nav_default_exit_anim"/>

    </fragment>

    <fragment
        android:id="@+id/fmt_b"
        android:name="com.mg.axechen.libnavigation.fragment.FragmentB"
        tools:layout="@layout/fragment_b"
        android:label="FragmentB" />

    <fragment
        android:id="@+id/fmt_c"
        android:name="com.mg.axechen.libnavigation.fragment.FragmentC"
        tools:layout="@layout/fragment_c"
        android:label="FragmentC" />

    <fragment
        android:id="@+id/fmt_d"
        android:name="com.mg.axechen.libnavigation.fragment.FragmentD"
        tools:layout="@layout/fragment_d"
        android:label="FragmentD" />
    private void initNavigation() {
        NavController navController = Navigation.findNavController(this, R.id.fmt_main);
        navController.navigate(R.id.fmt_d);

        bottomNavigationView = findViewById(R.id.btn_navigation_view);

        NavigationUI.setupWithNavController(bottomNavigationView, navController);
    }

这样就可以通过BottonNavigationView去控制Fragment的显示了。

4、defaultNavHost的使用

如果这个为true,则会将返回键交给Navigation进行管理,会覆盖Activity的回退事件,这样每次回退都会从栈中拿出上一个保存Fragment。

如果为false则不会覆盖Activity的回退事件。

这个属性可以在不同的场景中使用,假如使用BottonNavigationView+Navigation这种方式做主页,如果点击返回需要回到桌面,那可以将这个属性设置成false。

5、支持DeepLink

DeepLink实际上就是通过一种约定好的Url的方式直接定位的页面。


    <!-- 资源 -->
    <fragment
        android:id="@+id/fmt_a"
        android:name="com.mg.axechen.libnavigation.fragment.FragmentA"
        tools:layout="@layout/fragment_a"
        android:label="FramentA" >


        <deepLink app:uri="http://www.axechen.com/{params}"></deepLink>


    </fragment>

直接在fragment中配置deepLink即可,然后通过访问这个url即可直接打开这个Fragment。

6、传递参数

传递参数是通过Bundle来传递的,在接收的时候也是
Fragment通过getArguments()来接收。Activity通过Intent来接收

传参:

 Bundle bundle = new Bundle();
 bundle.putString("name","参数");

   Navigation.findNavController(getView())
                    .navigate(R.id.action_fmta_to_activity_demo,bundle);

Activity:

  String name = getIntent().getStringExtra("name");

Fragment:

 String params = getArguments().getString("name");

7、过渡动画

前面的配置中实际上已经配置了过渡动画了。动画配置在Action中。

   <action android:id="@+id/action_fmta_to_fmtb"
            app:destination="@id/fmt_b"
            app:enterAnim="@anim/nav_default_enter_anim"
            app:exitAnim="@anim/nav_default_exit_anim"/>

enterAnim和exitAnim就是过渡动画了。

以上就是Navigation的基本用法了,实际使用起来是非常简单的。

相关文章

网友评论

    本文标题:Jetpack第七篇:Navigation 组件

    本文链接:https://www.haomeiwen.com/subject/knormktx.html