『Do it! 깡샘의 안드로이드 앱 프로그래밍 with 코틀린』 교재를 바탕으로 정리한 내용입니다.
implementation 'com.google.android.material:material:1.4.0'
<com.google.android.material.appbar.AppBarLayout
android:id="@+id/appbar"
android:layout_width="match_parent"
android:layout_height="242dp"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar" >
</com.google.android.material.appbar.AppBarLayout>
<com.google.android.material.appbar.AppBarLayout
android:id="@+id/appbar"
android:layout_width="match_parent"
android:layout_height="242dp"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar" >
</com.google.android.material.appbar.AppBarLayout>
<com.google.android.material.appbar.AppBarLayout ... 생략 ... >
<androidx.appcompat.widget.Toolbar ... 생략 ... />
<ImageView ... 생략 ... />
</com.google.android.material.appbar.AppBarLayout>
implementation 'androidx.coordinatorlayout:coordinatorlayout:1.1.0'
<androidx.coordinatorlayout.widget.CoordinatorLayout ... 생략 ... >
<com.google.android.material.appbar.AppBarLayout ... 생략 ... >
<androidx.appcompat.widget.Toolbar
... 생략 ...
app:layout_scrollFlags="scroll|enterAlways"/>
<ImageView
... 생략 ...
app:layout_scrollFlags="scroll|enterAlways"/>
</com.google.android.material.appbar.AppBarLayout>
<androidx.core.widget.NestedScrollView
... 생략 ...
app:layout_behavior="@string/appbar_scrolling_view_behavior">
<TextView ... 생략 .../>
</androidx.core.widget.NestedScrollView>
</androidx.coordinatorlayout.widget.CoordinatorLayout>
<androidx.coordinatorlayout.widget.CoordinatorLayout ... 생략 ...>
<com.google.android.material.appbar.AppBarLayout ... 생략 ...>
<com.google.android.material.appbar.CollapsingToolbarLayout
android:contentScrim="?attr/colorPrimary"
app:expandedTitleMarginBottom="50dp"
app:expandedTitleMarginStart="48dp"
app:layout_scrollFlags="scroll|exitUntilCollapsed"
app:title="AppBarTitle">
<ImageView
... 생략 ...
app:layout_collapseMode="parallax"/>
<androidx.appcompat.widget.Toolbar
... 생략 ...
app:layout_collapseMode="pin"/>
</com.google.android.material.appbar.CollapsingToolbarLayout>
</com.google.android.material.appbar.AppBarLayout>
<androidx.recyclerview.widget.RecyclerView
... 생략 ...
app:layout_behavior="@string/appbar_scrolling_view_behavior" />
</androidx.coordinatorlayout.widget.CoordinatorLayout>
<com.google.android.material.appbar.CollapsingToolbarLayout ... 생략...
app:layout_scrollFlags="scroll|exitUntilCollapsed"
<ImageView ... 생략 ...
app:layout_collapseMode="parallax"/>
<androidx.appcompat.widget.Toolbar ... 생략 ...
app:app:layout_collapseMode="pin" />
</com.google.android.material.appbar.CollapsingToolbarLayout>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<com.google.android.material.tabs.TabLayout
android:id="@+id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<FrameLayout
android:id="@+id/tabContent"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
val tabLayout : TabLayout = findViewById(R.id.tabs)
val tab1: TabLayout.Tab = tabLayout.newTab()
tab1.text = "Tab1"
tabLayout.addTab(tab1)
val tab2: TabLayout.Tab = tabLayout.newTab()
tab2.text = "Tab2"
tabLayout.addTab(tab2)
val tab3: TabLayout.Tab = tabLayout.newTab()
tab3.text = "Tab3"
tabLayout.addTab(tab3)
<com.google.android.material.tabs.TabLayout
android:id="@+id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<com.google.android.material.tabs.TabItem
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Tab1" />
<com.google.android.material.tabs.TabItem
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Tab2" />
<com.google.android.material.tabs.TabItem
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Tab3" />
</com.google.android.material.tabs.TabLayout>
tabLayout.addOnTabSelectedListener(object : TabLayout.OnTabSelectedListener {
// 탭 버튼을 선택할 때 이벤트
override fun onTabSelected(tab: TabLayout.Tab?) {
val transaction = supportFragmentManager.beginTransaction()
when(tab?.text) {
"Tab1" -> transaction.replace(R.id.tabContent, OneFragment())
"Tab2" -> transaction.replace(R.id.tabContent, TwoFragment())
"Tab3" -> transaction.replace(R.id.tabContent, ThreeFragment())
}
transaction.commit()
}
// 선택된 탭 버튼을 다시 선택할 때 이벤트
override fun onTabReselected(tab: TabLayout.Tab?) { ... }
// 다른 탭 버튼을 눌러 선택된 탭 버튼이 해제될 때 이벤트
override fun onTabUnselected(tab: TabLayout.Tab?) { ... }
})
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<com.google.android.material.tabs.TabLayout
android:id="@+id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:tabMode="scrollable">
</com.google.android.material.tabs.TabLayout>
<androidx.viewpager2.widget.ViewPager2
android:id="@+id/viewpager"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
TabLayoutMediator(tabLayout, viewpager) { tab, position ->
tab.text = "Tab${(position + 1)}"
}.attach()
<androidx.drawerlayout.widget.DrawerLayout ... 생략 ... >
<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>
binding.mainDrawerView.setNavigationItemSelectedListener {
Log.d("kkang", "navigation item click... ${it.title}")
true
}
<com.google.android.material.floatingactionbutton.ExtendedFloatingActionButton
... 생략 ...
android:id="@+id/extendedFab
android:text="extended FAB"
app:icon="@android:drawable/ic_input_add"/>
binding.extendedFab.setOnClickListener { // 아이콘만 표시
when(binding.extendedFab.isExtended) { // 아이콘과 문자열 함께 표시했다면
true -> binding.extendedFab.shrink()
false -> binding.extendedFab.extend()
}
}