본문 바로가기

안드로이드

[안드로이드] 예제:MVVM+AAC를 이용한 RecyclerView 1

개요

본 문서에서는 MVVM(Model-View-ViewModel)과 AAC(Android Architecture Component)를 이용한 리사이클러뷰를 구현하는 것을 목표로 한다. Todo 리스트를 구현할 것이도 동적으로 추가, 삭제, 편집이 가능하다. 간단하게 구현결과를 보면 다음과 같다. 그리고 기본 언어는 kotlin을 활용한다.

구현 결과


전반적인 프로젝트 구조

 


구현 과정

1. 프로젝트 구성

2. Room 영속성 라이브러리 관련 클래스 구현

3. Repository 구현

4. ViewModel 구현

5. MainActivity 구현

6. RecyclerView 구현

7. AddActivity 구현

 

(구현 상세 내용 및 프로젝트 과정은 현재 대폭 수정되었습니다.- 2020.08.21)

 

1. 프로젝트 구성

우선, app단위의 build:gradle을 먼저 구성한다. 

apply plugin: 'kotlin-kapt' //코틀린 사용

android {
   ...
   //jvm 버젼 설정
    kotlinOptions {
        jvmTarget = "1.8"
    }

	//databinding 사용
    dataBinding {
        enabled = true
    }

}

dependencies {
    def lifecycle_version = "2.2.0"

    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
    implementation 'androidx.appcompat:appcompat:1.1.0'
    implementation 'androidx.core:core-ktx:1.3.1'
    implementation 'androidx.activity:activity-ktx:1.1.0'
    implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'androidx.test.ext:junit:1.1.1'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
    
    //Room
    implementation 'androidx.room:room-runtime:2.1.0'
    implementation "androidx.room:room-ktx:2.2.5"
     kapt 'androidx.room:room-compiler:2.1.0'
    
    //Lifecycle(coroutine)
    implementation 'androidx.lifecycle:lifecycle-runtime-ktx:2.2.0-alpha01'
    implementation 'androidx.lifecycle:lifecycle-viewmodel-ktx:2.2.0-alpha01'

	//recyclerview & cardview
    implementation 'com.android.support:recyclerview-v7:28.0.0'
    implementation 'com.android.support:cardview-v7:28.0.0'
}