5
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Navigation Architecture ComponentでDrawerに複数のルートをもたせる

Last updated at Posted at 2018-11-08

Android Architecture ComponentsのNavigation Component、少ないコードでDrawerの実装ができて便利です。ただし、少し前まではルートの要素をひとつしか持てないという成約がありました。

【参考記事】
https://www.slideshare.net/yasi_life/navigationui

1.0.0-alpha07にて、ついに複数のルート要素に対応したので対応方法をメモとして残しておきます。基本的な利用方法は他にも多数記事があるので差分箇所のみ。

導入

1.0.0-alpha07が必要なのでgradleの設定を修正します。

build.gradle
dependencies {
    implementation 'android.arch.navigation:navigation-fragment-ktx:1.0.0-alpha07'
    implementation 'android.arch.navigation:navigation-ui-ktx:1.0.0-alpha07'
}

NavControllerの設定

ポイントは、ルート要素のFragmentを引数に列挙したAppBarConfigurationを生成し、Toolbarに対してsetupWithNavControllerを実行する際に第2引数でそれを渡してあげる点です。Data Bindingを使ったサンプルになります。

MainActivity.kt
class MainActivity : AppCompatActivity() {
    private lateinit var binding: ActivityMainBinding
    private lateinit var navController: NavController

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = DataBindingUtil.setContentView(this, R.layout.activity_main)
        navController = findNavController(R.id.nav_host_fragment)
        val appBarConfig = AppBarConfiguration(setOf(
            R.id.fragment_root_a,
            R.id.fragment_root_b,
            R.id.fragment_root_c
        ), binding.drawerLayout)
        binding.toolbar.setupWithNavController(navController, appBarConfig)
        binding.navDrawer.setupWithNavController(navController)
    }

    override fun onSupportNavigateUp(): Boolean {
        return navController.navigateUp() || super.onSupportNavigateUp()
    }
}

参考までにlayoutファイルもサンプルを載せておきます。

layout/activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<layout
    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">
    <androidx.drawerlayout.widget.DrawerLayout
        android:id="@+id/drawer_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".MainActivity">
        <androidx.coordinatorlayout.widget.CoordinatorLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent">
            <com.google.android.material.appbar.AppBarLayout
                android:layout_height="wrap_content"
                android:layout_width="match_parent"
                android:theme="@style/AppTheme.AppBarOverlay">
                <androidx.appcompat.widget.Toolbar
                    android:id="@+id/toolbar"
                    android:layout_width="match_parent"
                    android:layout_height="?attr/actionBarSize"
                    android:background="?attr/colorPrimary"
                    app:popupTheme="@style/AppTheme.PopupOverlay"/>
            </com.google.android.material.appbar.AppBarLayout>
            <fragment
                android:id="@+id/nav_host_fragment"
                android:name="androidx.navigation.fragment.NavHostFragment"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                app:defaultNavHost="true"
                app:navGraph="@navigation/nav_graph"
                app:layout_behavior="@string/appbar_scrolling_view_behavior"/>
        </androidx.coordinatorlayout.widget.CoordinatorLayout>
        <com.google.android.material.navigation.NavigationView
            android:id="@+id/nav_drawer"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_gravity="start"
            app:headerLayout="@layout/nav_header_main"
            app:menu="@menu/nav_drawer"/>
    </androidx.drawerlayout.widget.DrawerLayout>
</layout>

参考URL

5
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?