Android Jetpack

Android Jetpack Navigation(一)搭载

2019-01-26  本文已影响0人  Rimson

Navigation

去年的Google I/O大会上,Google推出了Android Jetpack架构组件,Navigation就是其中之一。
本文主要介绍Navigation与BottomNavigationView的简单搭配使用,最后的效果就是这样:


注意:Android Studio版本需要在3.2及以上

一、创建Activity

上图是在一个Activity中显示了3个Fragment,第一步是创建这几个Fragment的容器。

activity_main.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 
    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:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".main.MainActivity">

    <fragment
        android:id="@+id/nav_host_fragment"
        android:name="androidx.navigation.fragment.NavHostFragment"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        app:navGraph="@navigation/nav_graph"
        app:defaultNavHost="true"/>

    <com.google.android.material.bottomnavigation.BottomNavigationView
        android:id="@+id/bottom_nav_view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:menu="@menu/navigation"/>
</LinearLayout>

fragment

BottomNavigationView

二、配置menu文件

  1. res目录下新建menu文件夹
  2. menu目录中,新建Menu resource file
  3. text标签中修改代码:
<menu xmlns:android="http://schemas.android.com/apk/res/android">
    <item
        android:id="@+id/tab1"
        android:icon="@mipmap/ic_launcher"
        android:title="Tab 1"/>
    <item
        android:id="@+id/tab2"
        android:icon="@mipmap/ic_launcher"
        android:title="Tab 2"/>
    <item
        android:id="@+id/tab3"
        android:icon="@mipmap/ic_launcher"
        android:title="Tab 3"/>
</menu>

三、配置navigation graph

  1. res目录下新建navigation文件夹
  2. navigation目录下,新建Navigation resource file
  3. 添加所需要导航的Fragment,注意不需要添加Action,不需要连接箭头
  4. 将每个Fragment对应的ID,修改为menu文件中item对应的ID(最重要的一步)

最终的nav_graph.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/nav_graph"
    app:startDestination="@id/tab1">

    <fragment
        android:id="@+id/tab1"
        android:name="..."
        android:label="..."
        tools:layout="@layout/fragment1">
    </fragment>

    <fragment
        android:id="@+id/tab2"
        android:name="..."
        android:label="..."
        tools:layout="@layout/fragment2" />

    <fragment
        android:id="@+id/tab3"
        android:name="..."
        android:label="..."
        tools:layout="@layout/fragment3">
    </fragment>

</navigation>

四、Navigation与BottomNavigationView适配

MainActivity.java中:

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment);
        BottomNavigationView bottomNav = findViewById(R.id.bottom_nav_view);
        NavigationUI.setupWithNavController(bottomNav, navController);
    }

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

注意:NavigationUI.setupWithNavController()方法,只有在onCreate()中调用才有效

上一篇 下一篇

猜你喜欢

热点阅读