반응형

이 게시물은 다음 링크 참조하여 학습했습니다.

 

탐색 구성요소 시작하기  |  Android 개발자  |  Android Developers

탐색 구성요소 시작하기 이 주제는 탐색 구성요소를 설정하고 사용하는 방법을 설명합니다. 탐색 구성요소의 대략적인 개요는 탐색 개요를 참고하세요. 환경 설정참고: 탐색 구성요소는 Android

developer.android.com

 

안드로이드(Android) - Navigation 사용법

[OverView] 이번에는 Navigation에 대해 알려드리겠습니다. Navigation는 iOS개발에서 사용하는 스토리보드와 흡사합니다. Fragment간의 플로우를 시각적으로 볼수 있다는 특징이 있어 앱 플로우를 보거나

jae-young.tistory.com

 

안드로이드 Navigation Components로 Fragment들을 전환해 보자.

Android Jetpack의 Navigation Component는 안드로이드 앱을 제작 할 때 화면간의 이동을 조금 더 쉽게 구현 할 수 있도록 도와줍니다. Navigation Component는 세 가지 주요 부분으로 구성 됩니다. Navigation G..

devlopsquare.tistory.com

 

프레그먼트 전용 컨테이너 FragmentContainerView | 찰스의 안드로이드

FragmentContainerView란? 액티비티에서 프래그먼트를 호스팅하는 일반적인 패턴 중 하나는 FrameLayout을 사용하는 것이다. androidx.fragment 1.2.0 에서는 FragmentContainerView라는 새로운 뷰를 도입했다. FragmentCo

www.charlezz.com

 

FragmentContainerView  |  Android Developers

FragmentContainerView public final class FragmentContainerView FragmentContainerView is a customized Layout designed specifically for Fragments. It extends FrameLayout, so it can reliably handle Fragment Transactions, and it also has additional features to

developer.android.com

 

이번 게시물은 Navigation 이다.

Navigation Component는 Fragment에서 화면간의 이동을 쉽게 구현하도록 도와주는 역할을 한다.

그리고 스토리보드 형식으로 전체적인 화면간의 흐름을 볼 수 있다.

 

1. FragmentContainerView

기존에 Fragment를 사용할 때 FrameLayout을 사용했는데, Navigation을 사용할 때는 FragmentContainerView를 사용한다.

FragmentContainerView는 FrameLayout의 보완된 버전이라 생각하면 된다.

트랜잭션을 안정적으로 처리할 수 있고, 프래그먼트 동작을 조정할 수 있다.

 

FragmentContainerView는 다음과 같이 사용한다.

1
2
3
4
5
6
7
8
9
<androidx.fragment.app.FragmentContainerView
            android:id="@+id/nav_host_fragment"
            android:name="androidx.navigation.fragment.NavHostFragment"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1"
 
            app:defaultNavHost="true"
            app:navGraph="@navigation/nav_graph" />
cs

android:name 속성에는 NavHostFragment를 넣어준다.

app:defaultNavHost 속성에는 host를 지정해주는 역할을 한다. system back button에 대한 권한을 가져온다고 한다.

app:navGraph 속성은 우리가 만들게 될 Navigation을 넣어주면 된다.

 

2. Navigation

네비게이션은 코드로 작성할수도 있고, 디자인 탭에서 스토리보드처럼 확인하고 수정할수도 있다.

nav_graph.xml

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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
<?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/nav_graph"
    app:startDestination="@id/fragment_1">
 
    <fragment
        android:id="@+id/fragment_1"
        android:name="com.example.navigationexample.Fragment1"
        android:label="Fragment1"
        tools:layout="@layout/fragment_1">
        <action
            android:id="@+id/action_fragment_1_to_fragment_2"
            app:destination="@id/fragment_2" />
        <action
            android:id="@+id/action_fragment_1_to_fragment_3"
            app:destination="@id/fragment_3" />
    </fragment>
 
    <fragment
        android:id="@+id/fragment_2"
        android:name="com.example.navigationexample.Fragment2"
        android:label="Fragment2"
        tools:layout="@layout/fragment_2">
        <action
            android:id="@+id/action_fragment_2_to_fragment_1"
            app:destination="@id/fragment_1" />
    </fragment>
 
    <fragment
        android:id="@+id/fragment_3"
        android:name="com.example.navigationexample.Fragment3"
        android:label="Fragment3"
        tools:layout="@layout/fragment_3">
 
        <action
            android:id="@+id/action_fragment_3_to_fragment_4"
            app:destination="@id/fragment_4" />
    </fragment>
 
    <fragment
        android:id="@+id/fragment_4"
        android:name="com.example.navigationexample.Fragment4"
        android:label="Fragment4"
        tools:layout="@layout/fragment_4">
 
    </fragment>
 
</navigation>
cs

nav_graph.xml에서 fragment를 작성해주고 디자인탭에서 화살표를 지정해 액션을 넣어주거나 코딩을 통해 넣어주면 된다.

startDestination 속성을 이용해 시작 프래그먼트를 지정해주거나 디자인탭에서 프래그먼트를 클릭하고 집모양 버튼을 눌러주면 시작 프래그먼트를 설정해줄 수 있다.

navigation 디자인 탭

 

3. Action

navigation에서 만든 action은 아래처럼 사용하면 된다.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
    lateinit var navHostFragment: NavHostFragment
    lateinit var navController: NavController
 
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = ActivityMainBinding.inflate(layoutInflater)
        setContentView(binding.root)
 
   navHostFragment = supportFragmentManager.findFragmentById(R.id.nav_host_fragment) as NavHostFragment
        navController = navHostFragment.navController
 
 
        binding.btnTest.setOnClickListener {
            navController.navigate(R.id.action_fragment_1_to_fragment_2)
        }
 
        binding.btnTest2.setOnClickListener {
            navController.navigate(R.id.action_fragment_2_to_fragment_1)
        }
 
    }
cs

1. 먼저 NavHostFragment를 supportFragmentManager.findFragmentById()를 통해 지정해주고,

2. NavController를 지정해준다.

3. NavController.navigate()에 action ID값을 넣어주면 action이 동작하게 된다.

 

Fragment에서 action을 사용하고 싶을때는 아래처럼 사용하면 된다.

1
2
3
4
5
override fun initView() {
        binding.btnF1.setOnClickListener {
            it.findNavController().navigate(R.id.action_fragment_1_to_fragment_3)
        }
    }
cs

 

반응형

+ Recent posts