-
Notifications
You must be signed in to change notification settings - Fork 0
Issue #6
dongk00 edited this page Jan 26, 2022
·
2 revisions
navigation.xml에 daily, monthly fragment를 각각 추가
-> content_main.xml + Navigation View(DrawerLayout으로 감싸고 드로어에 뜰 내용은 navigation view에 저장
-> headerLayout + menu
menu item id에 navigation에서 지정한 id를 지정해주면 navigation(fragment 이동)이 작동 (drawer_menu.xml)
layoutDirection을 right to left 로 해주면 드로어 오른쪽에서 사용 가능!
<activity_main.xml>
<androidx.drawerlayout.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:id="@+id/drawer_layout"
android:layoutDirection="rtl"
tools:openDrawer="start">
<include
android:id="@+id/content_main"
layout = "@layout/content_main"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
<com.google.android.material.navigation.NavigationView
android:id="@+id/navigation_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:fitsSystemWindows="true"
app:headerLayout="@layout/nav_header"
app:menu="@menu/drawer_menu"/>
</androidx.drawerlayout.widget.DrawerLayout>
-> Toolbar + FragmentContainerView(이거를 NavHostFragment, 움직일 최상단 프래그먼트의 host fragment로 지정)
<content_main.xml>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<androidx.coordinatorlayout.widget.CoordinatorLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<com.google.android.material.appbar.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<androidx.appcompat.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"/>
</com.google.android.material.appbar.AppBarLayout>
</androidx.coordinatorlayout.widget.CoordinatorLayout>
<androidx.fragment.app.FragmentContainerView
android:id="@+id/fragment_container_view"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:defaultNavHost="true"
app:navGraph="@navigation/navigation"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"/>
</LinearLayout>
여기서 지정한 navHostFragment(fragment_container_view) 는 MainActivity에서 navController 연결
actionbar는 theme에서 noactionbar로 지정, setsupportactionbar(toolbar)로 툴바를 액션바로 사용하게끔 지정
AppBarConfiguration : drawerlayout이 최상위 수준 대상을 찾아 up 버튼을 설정할지 안할지를 결정 -> setOf(nav_daily, nav_monthly)로 최상위 수준 fragment들을 직접 설정
<MainActivity.kt>
class MainActivity : AppCompatActivity() {
private val binding by lazy { ActivityMainBinding.inflate(layoutInflater) }
private lateinit var appBarConfiguration: AppBarConfiguration
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(binding.root)
setSupportActionBar(binding.contentMain.toolbar)
val navHostFragment = supportFragmentManager.findFragmentById(R.id.fragment_container_view) as NavHostFragment
val navController = navHostFragment.navController
appBarConfiguration = AppBarConfiguration(
setOf(
R.id.nav_daily_writing,
R.id.nav_monthly_writing
), binding.drawerLayout
)
binding.navigationView.setupWithNavController(navController)
setupActionBarWithNavController(navController, appBarConfiguration)
}
override fun onSupportNavigateUp(): Boolean {
val navController = findNavController(R.id.fragment_container_view)
return navController.navigateUp(appBarConfiguration) || super.onNavigateUp()
}
}