Android material : 네비게이션 뷰 - 드로어 화면 구성

timothy jeong·2021년 11월 11일
0

Android with Kotlin

목록 보기
32/69

지난 포스팅에서는 androidx 라이브러리의 드로어 레이아웃을 봤었다. 그때는 단순히 드로어 레이아웃에 TextView 만 넣었는데, 일반적으로 드로어에는 네비게이션 뷰를 넣는다.

<?xml version="1.0" encoding="utf-8"?>
<androidx.drawerlayout.widget.DrawerLayout 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">

    <LinearLayout ... />
       
    <com.google.android.material.navigation.NavigationView
        android:id="@+id/main_drawer_view"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        app:headerLayout="@layout/navigation_header"
        app:menu="@menu/menu_navigation"/>
</androidx.drawerlayout.widget.DrawerLayout>

드로어가 네비게이션 뷰를 갖도록 하였고, 헤더로는 res/layout 의 navigation_header 를 참조하도록 하였으며 메뉴로는 res/menu 의 menu_navigation 를 참조하도록 하였다.

네비게이션 뷰에서 메뉴를 클릭했을 때 이벤트는 setNavigationItemSelectedListener 함수를 구현하여 처리한다.

class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        val binding = ActivityMainBinding.inflate(layoutInflater)
        setContentView(binding.root)

        binding.mainDrawerView.setNavigationItemSelectedListener {
            Log.d("Info", "navigaion item click... ${it.title}")
            true
        }
    }
}
profile
개발자

0개의 댓글