Android Studio3.3 之 Navigation Editor 详解

AS3.3 版本之Navigation Editor(导航编辑器)

AS 3.3更新后,最大的看点就是Navigation Editor, 根据官方文档学习了一下,在这记录一下,帮助不能翻墙的同学学习这个新功能。
在这里插入图片描述

1. 导入Navigation Editor

在app中的build.gradle中加入

    def nav_version = "1.0.0-alpha09"
    implementation "android.arch.navigation:navigation-fragment:$nav_version" // use -ktx for Kotlin
    implementation "android.arch.navigation:navigation-ui:$nav_version" // use -ktx for Kotlin

2. 创建资源文件
在res文件夹中新建navigation文件夹,文件夹名字注意别写错(navigation),在navigation中新建nav_graph.xml文件,此文件为导航配置文件。
在这里插入图片描述

<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/nav_graph"
    app:startDestination="@id/mainFragment">
  <!--- 省略fragment 配置-->
</navigation>

android:id="@+id/nav_graph" 为自动生成的,id即为文件名。

3. 创建HOST
在activity_main.xml 中添加fragment标签

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">
    <fragment
        android:id="@+id/nav_host_fragment"
        android:name="androidx.navigation.fragment.NavHostFragment"
        android:layout_width="0dp"
        android:layout_height="0dp"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"
        app:defaultNavHost="true"
        app:navGraph="@navigation/nav_graph" />

</android.support.constraint.ConstraintLayout>

关键代码:

           android:id="@+id/nav_host_fragment"
           android:name="androidx.navigation.fragment.NavHostFragment"
           app:defaultNavHost="true"
           app:navGraph="@navigation/nav_graph"

app:defaultNavHost=“true” 设置此界面为HOST,还可以在Activity代码中使用以下代码

@Override
public boolean onSupportNavigateUp() {
    return Navigation.findNavController(this, R.id.nav_host_fragment).navigateUp();
}

或者

NavHostFragment finalHost = NavHostFragment.create(R.navigation.nav_graph);
getSupportFragmentManager().beginTransaction()
    .replace(R.id.nav_host_fragment, finalHost)
    .setPrimaryNavigationFragment(finalHost) // this is the equivalent to app:defaultNavHost="true"
    .commit();

4. 创建指向链

点击design 标签,进入desgin 模式。点击加号创建fragment。选中某个fragment,连接到指定fragment
在这里插入图片描述
在这里插入图片描述
代码如图在这里插入图片描述

<?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/nav_graph"
    app:startDestination="@id/mainFragment">
    <fragment
        android:id="@+id/mainFragment"
        android:name="com.example.myapplication.MainFragment"
        android:label="fragment_blank"
        tools:layout="@layout/fragment_main" >
        <action
            android:id="@+id/action_mainFragment_to_stepOneFragment"
            app:destination="@id/stepOneFragment" />
        <action
            android:id="@+id/action_mainFragment_to_stepTwoFragment"
            app:destination="@id/stepTwoFragment" />
    </fragment>
    <fragment
        android:id="@+id/stepOneFragment"
        android:name="com.example.myapplication.StepOneFragment"
        android:label="fragment_blank_two"
        tools:layout="@layout/fragment_step_one" >
        <action
            android:id="@+id/action_blankTwoFragment_to_stepThreeFragment2"
            app:destination="@id/stepThreeFragment" />
    </fragment>
    <fragment
        android:id="@+id/stepTwoFragment"
        android:name="com.example.myapplication.StepTwoFragment"
        android:label="stepTwoFragment"
        tools:layout="@layout/fragment_step_two" />
    <fragment
        android:id="@+id/stepThreeFragment"
        android:name="com.example.myapplication.StepThreeFragment"
        android:label="fragment_step_three"
        tools:layout="@layout/fragment_step_three" />
</navigation>

关键代码
<action android:id="@+id/action_mainFragment_to_stepOneFragment" app:destination="@id/stepOneFragment" /> <action android:id="@+id/action_mainFragment_to_stepTwoFragment" app:destination="@id/stepTwoFragment" />
action 为关键标签,有id 和app:destination,id在跳转时使用, app:destination指定跳转目标fragment。

5. 代码中的界面跳转处理

fragment_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainFragment">

    <!-- TODO: Update blank fragment layout -->
    <!--<TextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:text="@string/hello_blank_fragment" />-->
    <Button
        android:id="@+id/next_btn"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="click me"/>
    <Button
        android:id="@+id/next_btn2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="click me 2"/>
</LinearLayout>

MainFragment.java

 Button btn1 = view.findViewById(R.id.next_btn);

        btn1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view1) {
                NavHostFragment.findNavController(MainFragment.this).navigate(R.id.action_mainFragment_to_stepOneFragment);
            }
        });
        Button btn2 = view.findViewById(R.id.next_btn2);
        btn2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                NavHostFragment.findNavController(MainFragment.this).navigate(R.id.action_mainFragment_to_stepTwoFragment);
            }
        });

跳转到指定界面,必须使用NavController类, NavController有以下几种获取方式

  • NavHostFragment.findNavController(Fragment)
  • Navigation.findNavController(Activity, @IdRes int viewId)
  • Navigation.findNavController(View)

官方解释

After you retrieve a NavController, use its navigate() method to navigate to a destination. The navigate() method accepts a resource ID. The ID can be the ID of a specific destination in the navigation graph or of an action. Using the ID of the action, instead of the resource ID of the destination, has advantages, such as associating transitions with your navigation. For more on transitions, refer to Create a transition between destinations.
大概意思是使用navigate()进行界面跳转时,可以传入resource id 和action id,但是此id必须在导航配置xml文件中,在此项目中为nav_graph.xml。推荐使用actionId ,可以配合过渡动画使用。

6. 创建deep link(外部通过url来启动某个界面)
deep link 是app内的一个url,,例如www.cashdog.com,自动转换成http://www.cashdog.com 和https://www.cashdog.com。AS自带验证功能,验证此url是否是你的。
在这里插入图片描述

<deepLink
        android:id="@+id/deepLink"
        app:uri="http://www.baidu.com"
        android:autoVerify="false" />
 <activity
            android:name=".MainActivity"
            android:label="@string/app_name">
            <nav-graph android:value="@navigation/main_nav" />
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

END

  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值