๐Ÿ”ฅTIL๐Ÿ”ฅ์ŠคํŒŒ๋ฅดํƒ€ | ํŠน์ • ํ™”๋ฉด์—์„œ BottomNavigation์˜ FloatingActionButton ์•ˆ๋ณด์ด๊ฒŒ ํ•˜๊ธฐ

hyihyiยท2024๋…„ 3์›” 6์ผ
0

TIL

๋ชฉ๋ก ๋ณด๊ธฐ
58/69
post-thumbnail

๋‚˜๋Š” MainActivity์—์„œ BottomAppbar + FloatingActionButton์„ ์‚ฌ์šฉํ–ˆ๋‹ค.
BottomNavigation ์—๋Š” ์ด 4๊ฐœ์˜ ํƒญ์ด ์žˆ๋‹ค.
ํ™ˆ ํ™”๋ฉด์—์„œ๋Š” 4๊ฐœ์˜ ํƒญ์ด ๋‹ค ๋ณด์ด๊ณ  2๋ฒˆ์งธ ํƒญ์—์„œ๋Š” BottomNavigation ๊ฐ€์šด๋ฐ์— ๋œจ๋Š” FloatingActionButton์„ ์•ˆ ๋ณด์ด๊ฒŒ ํ•˜๊ณ  ์‹ถ์—ˆ๋‹ค.

MainActivity์—์„œ NavController์˜ addOnDestinationChangedListener๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ํ˜„์žฌ ํ™œ์„ฑํ™”๋œ ํ”„๋ž˜๊ทธ๋จผํŠธ์— ๋”ฐ๋ผ ๊ฐ€์‹œ์„ฑ์„ ์กฐ์ ˆํ•œ๋‹ค.

  • ์ž˜๋ชป๋œ ID ์‚ฌ์šฉ

  • ์˜ฌ๋ฐ”๋ฅธ ๋„ค๋น„๊ฒŒ์ด์…˜ ๊ทธ๋ž˜ํ”„์˜ ID ์‚ฌ์šฉ

์ฝ”๋“œ

MainActivity

private fun setNavigation() {
    val navHomeFragment =
        supportFragmentManager.findFragmentById(R.id.nav_main_fragment) as NavHostFragment
    val navController = navHomeFragment.navController
    binding.bottomNavigationView.setupWithNavController(navController)

    navController.addOnDestinationChangedListener { _, destination, _ ->
        when (destination.id) {
            R.id.navigation_map -> binding.fab.visibility = View.GONE // ๋„ค๋น„๊ฒŒ์ด์…˜ ๊ทธ๋ž˜ํ”„์˜ ID
            else -> binding.fab.visibility = View.VISIBLE // ๊ทธ ์™ธ์˜ ๊ฒฝ์šฐ FAB ํ‘œ์‹œ
        }
    }
}

โ›” ์ฃผ์˜ํ•  ์ 

์œ„์— ๋“ค์–ด๊ฐ€๋Š” navigation_map๋Š” ํ”„๋ž˜๊ทธ๋จผํŠธ์˜ ID๊ฐ€ ์•„๋‹ˆ๋ผ ๋„ค๋น„๊ฒŒ์ด์…˜ ๊ทธ๋ž˜ํ”„์— ์ •์˜๋œ ๋Œ€์ƒ์˜ ID๋ฅผ ์‚ฌ์šฉํ•ด์•ผ ํ•œ๋‹ค.

profile
์ž์œ ๋กญ๊ฒŒ ์“ด ๋‚˜์˜ ์ž์œ ๋กœ์šด Development voyageโ›ต

0๊ฐœ์˜ ๋Œ“๊ธ€