8
7

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.

特定のFragmentが表示される時にAppBarLayoutとBottomNavigationViewを非表示にする

Posted at

Navigationで画面遷移を実装している場合、こんなふうに実現できます。
Fragment側からActivityのリソースにアクセスする必要がないので、Fragmentの独立性が維持できていいですね。

MainActivity.kt
private fun setupBottomNavigationBar() {

    val navGraphIds =
        listOf(R.navigation.tab_1, R.navigation.tab_2, R.navigation.tab_3)

    val controller = bottomNavigation.setupWithNavController(
        navGraphIds = navGraphIds,
        fragmentManager = supportFragmentManager,
        containerId = R.id.nav_host_container,
        intent = intent
    )
    controller.observe(this, Observer { navController ->
        if (navController.graph.id == R.id.tab_1) {
            navController.addOnDestinationChangedListener { controller, destination, arguments ->
                val s = resources.getResourceName(destination.id)
                info { "resourceName:$s" }
		// tab1でFullScreenFragmentが表示される時に、AppBarLayoutとBottomNavigationViewを非表示
                if (destination.id == R.id.fullScreenFragment) {
                    bottomNavigation.visibility = View.GONE
                    toolbar.visibility = View.GONE
                    appBarLayout.setExpanded(false, true)
                } else {
                    bottomNavigation.visibility = View.VISIBLE
                    toolbar.visibility = View.VISIBLE
                    appBarLayout.setExpanded(true, true)
                }
            }
            setupActionBarWithNavController(navController)
        }
    })
    currentNavController = controller
}


8
7
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
8
7

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?