일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | ||||
4 | 5 | 6 | 7 | 8 | 9 | 10 |
11 | 12 | 13 | 14 | 15 | 16 | 17 |
18 | 19 | 20 | 21 | 22 | 23 | 24 |
25 | 26 | 27 | 28 | 29 | 30 | 31 |
- spring
- 운영체제
- SOLID
- design pattern
- coroutine flow
- 더티비트
- 리드미
- android study jam
- Compose
- 마크다운
- 데드락
- github
- 다단계 큐
- readme
- Constraint Layout
- 깃허브
- LiveData
- JUnit
- Class.class
- JetPack
- OS
- markdown
- kotlin
- O.S
- test
- Android
- Data Binding
- Spring Boot
- Di
- git
- Today
- Total
차지
[Android Study Jam] Unit 4: Navigation, Fragment 본문
유닛 4에서는 Fragment화면을 오가는 navigation에 대해서 공부합니다.
Fragment
Fragment는 앱 UI의 재사용 가능한 부분을 나타냅니다.
자체 레이아웃을 정의 및 관리하고 자체 lifecycle을 보유하며 자체 입력 이벤트를 처리할 수 있습니다.
프래그먼트는 독립적으로 존재할 수 없습니다.
우선 프래그먼트를 추가해 activity_main.xml의 레이아웃에 넣습니다.
만든 TitleFragment 클래스에 onCreate() 메소드를 onCreateView()메소드로 변경합니다.
class TitleFragment : Fragment() {
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
}
}
layout file에 fragment를 추가시킵니다.
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<fragment
android:id="@+id/titleFragment"
android:name="com.example.android.navigation.TitleFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
</LinearLayout>
</layout>
Navigation
fragment를 추가했으니 액티비티 내에서 특정 행동을 취하면 특정 fragment로 이동할 수 있게 해줘야합니다.
의존성을 추가하고, 그래프를 추가하는 과정으로 손쉽게 fragment를 사용할 수 있습니다.
먼저 build.gradle에서 의존성을 추가합니다.
ext {
...
navigationVersion = "2.3.0"
...
}
dependencies {
...
implementation "androidx.navigation:navigation-fragment-ktx:$navigationVersion"
implementation "androidx.navigation:navigation-ui-ktx:$navigationVersion"
...
}
그리고 navigation graph를 프로젝트에 추가합니다.
화면 전환을 디자인 에디터에서 정리할 수 있습니다.
- 참고
developer.android.com/courses/study-jams
Android Study Jams | Android 개발자 | Android Developers
Become an Android Developer. Learn how to build Android apps in Kotlin by following an online curriculum together with a study group.
developer.android.com
developer.android.com/guide/fragments
'Android > Android' 카테고리의 다른 글
[Android] MVC 구조 (0) | 2021.09.21 |
---|---|
[Android] Collection 함수 (0) | 2021.07.24 |
[Android Study Jam] Unit 5: Lifecycles and logging (0) | 2021.04.13 |
[Android Study Jam] Unit 3: Layout, Data binding (0) | 2021.04.09 |
[Android Study Jam]UNIT 2: Dice Roller (0) | 2021.04.07 |