๐Ÿ”ฅTIL๐Ÿ”ฅ์ŠคํŒŒ๋ฅดํƒ€ | BottomNavigation + NavGraph

hyihyiยท2024๋…„ 2์›” 26์ผ
0

TIL

๋ชฉ๋ก ๋ณด๊ธฐ
54/70
post-thumbnail

๐Ÿ“Œ MainActivity์œ„์— SearchFragment/FavoritesFragment ์˜ฌ๋ฆฌ๊ธฐ

1. nav_graph ์ถ”๊ฐ€

navigation/nav_graph

<?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/mobile_navigation"
    app:startDestination="@id/navigation_search">

    <fragment
        android:id="@+id/navigation_search"
        android:name="com.example.navgraph.SearchFragment"
        android:label="search_fragment"
        tools:layout="@layout/fragment_search">
        <action
            android:id="@+id/action_navigation_search_to_navigation_track_detail"
            app:destination="@id/navigation_favorites" />
    </fragment>

    <fragment
        android:id="@+id/navigation_favorites"
        android:name="com.example.navgraph.FavoritesFragment"
        android:label="favorites_fragment"
        tools:layout="@layout/fragment_favorites">
        <action
            android:id="@+id/action_navigation_track_detail_to_navigation_search"
            app:destination="@id/navigation_search" />
    </fragment>

</navigation>

2. nav_menu ์ถ”๊ฐ€

menu/nav_menu

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">

    <item
        android:id="@+id/navigation_search"
        android:icon="@drawable/ic_search"
        android:title="๋…ธ๋ž˜ ๊ฒ€์ƒ‰" />
    <item
        android:id="@+id/navigation_favorites"
        android:icon="@drawable/ic_favorites"
        android:title="๋‚ด ๋ณด๊ด€ํ•จ" />

</menu>

3. activity_main.xml ์ถ”๊ฐ€

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <androidx.fragment.app.FragmentContainerView
        android:id="@+id/nav_host_fragment"
        android:name="androidx.navigation.fragment.NavHostFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:defaultNavHost="true"
        app:navGraph="@navigation/nav_graph"/>

    <com.google.android.material.bottomnavigation.BottomNavigationView
        android:id="@+id/bottom_navigation"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:menu="@menu/nav_menu" />

</androidx.constraintlayout.widget.ConstraintLayout>

1.app:defaultNavHost="true"
2.app:navGraph="@navigation/nav_graph"

  • app:defaultNavHost="true"
    : ์ด ์†์„ฑ์€ ํ˜„์žฌ FragmentContainerView๊ฐ€ ์•ฑ ๋‚ด์˜ ๊ธฐ๋ณธ ๋„ค๋น„๊ฒŒ์ด์…˜ ํ˜ธ์ŠคํŠธ๋กœ ์ž‘๋™ํ•ด์•ผ ํ•จ์„ ๋‚˜ํƒ€๋ƒ„
    ์ฆ‰, ์‚ฌ์šฉ์ž๊ฐ€ Android ๊ธฐ๊ธฐ์˜ ๋ฐฑ ๋ฒ„ํŠผ์„ ๋ˆ„๋ฅด๊ฑฐ๋‚˜, ์•ก์…˜ ๋ฐ”์˜ 'Up' ๋ฒ„ํŠผ์„ ์‚ฌ์šฉํ•  ๋•Œ, Navigation Component๊ฐ€ ์ •์˜ํ•œ ๋Œ€๋กœ ํ”„๋ž˜๊ทธ๋จผํŠธ ๊ฐ„ ํƒ์ƒ‰์ด ์ด๋ฃจ์–ด์ง‘๋‹ˆ๋‹ค.
  • app:navGraph="@navigation/nav_graph"
    : ์ด ์†์„ฑ์€ FragmentContainerView์— ์‚ฌ์šฉํ•  ๋„ค๋น„๊ฒŒ์ด์…˜ ๊ทธ๋ž˜ํ”„๋ฅผ ์ง€์ •

๋ฐ˜๋“œ์‹œ ๋™์‹œ์— ์‚ฌ์šฉํ•  ํ•„์š”๋Š” ์—†์ง€๋งŒ, ์ผ๋ฐ˜์ ์œผ๋กœ ํ•จ๊ป˜ ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค. ์ด ๋‘ ์†์„ฑ์ด ํ•จ๊ป˜ ์ž‘๋™ํ•  ๋•Œ, FragmentContainerView ๋˜๋Š” NavHostFragment๊ฐ€ ์•ฑ ๋‚ด์˜ ๋„ค๋น„๊ฒŒ์ด์…˜ ํ๋ฆ„์„ ํšจ์œจ์ ์œผ๋กœ ๊ด€๋ฆฌํ•  ์ˆ˜ ์žˆ๊ธฐ ๋•Œ๋ฌธ

4. MainActivity ์ˆ˜์ •

class MainActivity : AppCompatActivity() {

    private val binding: ActivityMainBinding by lazy {
        ActivityMainBinding.inflate(layoutInflater)
    }
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(binding.root)

        val navHostFragment = supportFragmentManager.findFragmentById(R.id.nav_host_fragment) as NavHostFragment
        val navController = navHostFragment.navController
        binding.bottomNavigation.setupWithNavController(navController)

    }
}
  • setupWithNavController
    : BottomNavigationView์™€ NavController๋ฅผ ์—ฐ๊ฒฐํ•˜๋Š” ์—ญํ• 
    BottomNavigationView์˜ ํ•ญ๋ชฉ ์„ ํƒ์ด NavController์˜ ๋„ค๋น„๊ฒŒ์ด์…˜ ์•ก์…˜๊ณผ ๋™๊ธฐํ™”๋˜์–ด, ์‚ฌ์šฉ์ž๊ฐ€ ํ•˜๋‹จ ํƒญ์„ ํด๋ฆญํ•  ๋•Œ ํ•ด๋‹นํ•˜๋Š” ํ”„๋ž˜๊ทธ๋จผํŠธ๋กœ ์ž๋™์œผ๋กœ ํƒ์ƒ‰
  1. ํƒ์ƒ‰ ์—ฐ๊ฒฐ : BottomNavigationView์˜ ๊ฐ ๋ฉ”๋‰ด ํ•ญ๋ชฉ์ด NavController์˜ ๋„ค๋น„๊ฒŒ์ด์…˜ ๊ทธ๋ž˜ํ”„์— ์ •์˜๋œ ๋ชฉ์ ์ง€์™€ ์—ฐ๊ฒฐ
  2. ์ž๋™ ํƒ์ƒ‰ ์ฒ˜๋ฆฌ : ์‚ฌ์šฉ์ž๊ฐ€ BottomNavigationView์˜ ํ•ญ๋ชฉ์„ ์„ ํƒํ•˜๋ฉด, NavController๋Š” ํ•ด๋‹น ๋ชฉ์ ์ง€๋กœ ์ž๋™์œผ๋กœ ํƒ์ƒ‰
  3. ํƒ์ƒ‰ ์ƒํƒœ ๋ฐ˜์˜ : NavController์˜ ํ˜„์žฌ ๋„ค๋น„๊ฒŒ์ด์…˜ ์ƒํƒœ๊ฐ€ ๋ณ€๊ฒฝ๋˜๋ฉด, BottomNavigationView๋Š” ์ด๋ฅผ ๋ฐ˜์˜ํ•˜์—ฌ ํ˜„์žฌ ์„ ํƒ๋œ ํ•ญ๋ชฉ์„ ๊ฐ•์กฐ ํ‘œ์‹œ

โ— ์ฃผ์˜ํ•  ์ 

1.๊ฐ ๋ฉ”๋‰ด ํ•ญ๋ชฉ์˜ id๊ฐ€ ๋„ค๋น„๊ฒŒ์ด์…˜ ๊ทธ๋ž˜ํ”„์˜ ๋ชฉ์ ์ง€ id์™€ ์ผ์น˜ํ•ด์•ผ ํ•จ

<?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/mobile_navigation"
    app:startDestination="@id/navigation_search">

    <fragment
        android:id="@+id/navigation_search"
        android:name="com.example.navgraph.SearchFragment"
        android:label="search_fragment"
        tools:layout="@layout/fragment_search">
        <action
            android:id="@+id/action_navigation_search_to_navigation_track_detail"
            app:destination="@id/navigation_favorites" />
    </fragment>

    <fragment
        android:id="@+id/navigation_favorites"
        android:name="com.example.navgraph.FavoritesFragment"
        android:label="favorites_fragment"
        tools:layout="@layout/fragment_favorites">
        <action
            android:id="@+id/action_navigation_track_detail_to_navigation_search"
            app:destination="@id/navigation_search" />
    </fragment>

</navigation>

์œ„์˜ ์ฝ”๋“œ์—์„œ id, navigation_search์™€ navigation_favorites๊ฐ€ ์•„๋ž˜ ์ฝ”๋“œ์—์„œ id๋ž‘ ๊ฐ™์•„์•ผ ํ•จ

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">

    <item
        android:id="@+id/navigation_search"
        android:icon="@drawable/ic_search"
        android:title="๋…ธ๋ž˜ ๊ฒ€์ƒ‰" />
    <item
        android:id="@+id/navigation_favorites"
        android:icon="@drawable/ic_favorites"
        android:title="๋‚ด ๋ณด๊ด€ํ•จ" />

</menu>

2.name, defaultNavHost, navGraph ์†์„ฑ ์ž…๋ ฅ

android:name="androidx.navigation.fragment.NavHostFragment"
์ด๋ž‘
app:defaultNavHost="true"
app:navGraph="@navigation/nav_graph"
์ž…๋ ฅํ•ด์ค˜์•ผ ํ•จ

๊นƒํ—ˆ๋ธŒ ๋งํฌ

profile
๋‚ด๊ฐ€ ์ดํ•ดํ•˜๊ธฐ ์‰ฝ๊ฒŒ ์“ฐ๋Š” ๋ธ”๋กœ๊ทธ

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