๊ณ ๋„๊ณ ๋„
๐ŸŽ๐Ÿ
๊ณ ๋„๊ณ ๋„
์ „์ฒด ๋ฐฉ๋ฌธ์ž
7,127
์˜ค๋Š˜
0
์–ด์ œ
11
  • ๋ถ„๋ฅ˜ ์ „์ฒด๋ณด๊ธฐ (144)
    • ๐Ÿ”จ ํ”„๋กœ์ ํŠธ (0)
      • TP 1 (0)
      • WhiteHCCTV (0)
      • FootPrint (0)
    • ๐Ÿ’ป ๊ฐœ๋ฐœ (38)
      • TIL (16)
      • Android (6)
      • Kotlin (4)
      • Flutter (5)
      • Node.js (5)
      • Error (2)
    • โœ๏ธ ์•Œ๊ณ ๋ฆฌ์ฆ˜ (6)
      • Graph (6)
      • String (0)
      • Sort (0)
    • โœ๏ธ ์ฝ”ํ…Œ ์ค€๋น„ (44)
      • Math (1)
      • Implementation (3)
      • String (3)
      • Brute Force (5)
      • Back Tracking (7)
      • Greedy (0)
      • Dynamic Programming (13)
      • Binary Search (1)
      • DFS, BFS (5)
      • Shortest Path (2)
      • Two Pointer (4)
      • MST (0)
    • ๐Ÿ“š CS (6)
      • Operating System (6)
    • โ›น๏ธ ๋ผ์ดํ”„ (50)
      • 2020 ๊ฒจ์šธ๋ฐฉํ•™ ๋ชจ์นต์ฝ”(ํŒ€) (13)
      • 2020 ๊ฒจ์šธ๋ฐฉํ•™ ๋ชจ๊ฐ์ฝ”(๊ฐœ์ธ) (13)
      • 2021 ์—ฌ๋ฆ„๋ฐฉํ•™ ๋ชจ์นต์ฝ”(ํŒ€) (8)
      • 2021 ์—ฌ๋ฆ„๋ฐฉํ•™ ๋ชจ๊ฐ์ฝ”(๊ฐœ์ธ) (7)
      • ์ฝ”๋”ฉ ํ…Œ์ŠคํŠธ (1)
      • ํšŒ๊ณ  (7)

๋ธ”๋กœ๊ทธ ๋ฉ”๋‰ด

  • ํ™ˆ
  • ๊นƒํ—ˆ๋ธŒ

์ธ๊ธฐ ๊ธ€

  • [TIL] 22.03.13
    2022.03.13
    [TIL] 22.03.13
  • [๋ฌธ์ž์—ด / Kotlin] 2020 KAKAOโ‹ฏ
    2022.03.11
    [๋ฌธ์ž์—ด / Kotlin] 2020 KAKAOโ‹ฏ
  • [TIL] 22.03.20
    2022.03.20
    [TIL] 22.03.20
  • [ํผ๋“€ ์ผ๊ธฐ] ์–ด๋А๋ง ํ•œ ๋‹ฌ์งธ
    2022.03.01
    [ํผ๋“€ ์ผ๊ธฐ] ์–ด๋А๋ง ํ•œ ๋‹ฌ์งธ
  • [๊ตฌํ˜„ / Kotlin] 2022 SK ICT Fโ‹ฏ
    2022.03.12
    [๊ตฌํ˜„ / Kotlin] 2022 SK ICT Fโ‹ฏ

์ตœ๊ทผ ๊ธ€

  • [ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค] 124 ๋‚˜๋ผ์˜ ์ˆซ์ž
    2022.06.09
    [ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค] 124 ๋‚˜๋ผ์˜ ์ˆซ์ž
  • [LeetCode] 167. Two Sum II -โ‹ฏ
    2022.06.09
    [LeetCode] 167. Two Sum II -โ‹ฏ
  • [Android] ์•Œ๋ฆผ ํด๋ฆญ์‹œ Activitโ‹ฏ
    2022.06.08
    [Android] ์•Œ๋ฆผ ํด๋ฆญ์‹œ Activitโ‹ฏ
  • [Android] ๋ฐฑ๊ทธ๋ผ์šด๋“œ์—์„œ ์†Œ์ผ“โ‹ฏ
    2022.06.08
    [Android] ๋ฐฑ๊ทธ๋ผ์šด๋“œ์—์„œ ์†Œ์ผ“โ‹ฏ
  • [TIL] 22.06.03 - iOS ํ™”๋ฉด ์ „ํ™˜
    2022.06.03
    [TIL] 22.06.03 - iOS ํ™”๋ฉด ์ „ํ™˜

์ตœ๊ทผ ๋Œ“๊ธ€

  • ์ž˜๋ณด๊ณ  ๊ฐ‘๋‹ˆ๋‹ค~
    ๊ฐœ๊ฐ“์„ธ
  • ์ข‹์€ ๊ธ€์ด๋„ค์š”
    ๋‚ ์ธ๋กœ์„ธ
  • ์ž˜ ๋ณด๊ณ  ๊ฐ‘๋‹ˆ๋‹ค^^
    ํ”„๋กœํผํ‹ฐ
hELLO ยท Designed By ์ •์ƒ์šฐ.
๊ณ ๋„๊ณ ๋„

๐ŸŽ๐Ÿ

[Android] MVVM ํŒจํ„ด ์ ์šฉ๊ธฐ - 1
๐Ÿ’ป ๊ฐœ๋ฐœ/Android

[Android] MVVM ํŒจํ„ด ์ ์šฉ๊ธฐ - 1

2022. 4. 11. 20:40
 

GitHub - k906506/MVVM: ๐Ÿ”จ MVVM์ด ๋ญ์—์š”?

๐Ÿ”จ MVVM์ด ๋ญ์—์š”? Contribute to k906506/MVVM development by creating an account on GitHub.

github.com

 

์š”์ฆ˜ MVVM ์— ๋Œ€ํ•ด์„œ ๊ณต๋ถ€ํ•˜๊ณ  ์žˆ๋‹ค. ์ง€๊ธˆ๊นŒ์ง€ ์•ฑ ๊ตฌํ˜„ํ•  ๋•Œ ๊ทธ๋ƒฅ ์•กํ‹ฐ๋น„ํ‹ฐ์— ๋‹ค ๋•Œ๋ ค๋ฐ•์•˜์—ˆ๋Š”๋ฐ ๋ชจ๋ธ์ด๋ž‘ ๋ทฐ๊ฐ€ ๋„ˆ๋ฌด ๊ฐ•ํ•˜๊ฒŒ ๊ฒฐํ•ฉ๋˜์–ด ์žˆ๋Š” ๋А๋‚Œ์ด ๊ฐ•ํ–ˆ๋‹ค. ๋˜ํ•œ ๋Œ€๋ถ€๋ถ„์˜ ๋กœ์ง์„ ๋ทฐ(์•กํ‹ฐ๋น„ํ‹ฐ)์—์„œ ์ฒ˜๋ฆฌํ•˜๊ณ  ํ•˜๊ณ  ์žˆ์—ˆ๊ณ  ์žฌ์‚ฌ์šฉ์„ฑ์ด ๋งค์šฐ ๋–จ์–ด์กŒ์—ˆ๋‹ค. ์‹ค์ œ๋กœ ๊ฒจ์šธ๋ฐฉํ•™์— ์ง„ํ–‰ํ–ˆ๋˜ ๊ธ€๋กœ๋ฒŒ์ธ์žฌํŠธ๋ž™ ๋•Œ ์–ดํ”Œ ์„ ํ•˜๋‚˜ ๋งŒ๋“ค์—ˆ๊ณ  ๋‚˜๋ฆ„ ๋ถ„๋ฆฌํ•œ๋‹ค๊ณ  ๋ถ„๋ฆฌํ–ˆ๋Š”๋ฐ ์ง„์งœ ์žฌ์‚ฌ์šฉ์„ฑ์ด 0 ์ด์—ˆ๋‹ค. MVC ๊ฐ€ ๊ตฌํ˜„ํ•˜๊ธฐ๋Š” ์ •๋ง ํŽธํ–ˆ์ง€๋งŒ ์ด ๊ธฐํšŒ์— ํŒจํ„ด ๊ณต๋ถ€๋„ ํ• ๊ฒธ MVVM ์„ ์ ‘ํ•˜๊ฒŒ ๋˜์—ˆ๋‹ค. ๋ฌผ๋ก  MVC ๋ณด๋‹ค MVVM ์ด ๊ตฌํ˜„ํ•˜๊ธฐ ์–ด๋ ต๋‹ค. ๊ทธ๋ ‡์ง€๋งŒ ๋ถˆํ–‰ ์ค‘ ๋‹คํ–‰! ๊ตฌ๊ธ€์—์„œ MVVM ์„ ์‰ฝ๊ฒŒ ๊ตฌํ˜„ํ•  ์ˆ˜ ์žˆ๋„๋ก AAC(Android Architecture Component) ๋ฅผ ์ง€์›ํ•˜๊ณ  ์žˆ๋‹ค.

๊ทธ๋ž˜์„œ MVVM์ด ๋ญ์—์š”?

MVVM์€ Model, View, View Model ๋กœ ๋ถ„๋ฆฌ๋œ ์•„ํ‚คํ…์ณ ๋””์ž์ธ ํŒจํ„ด ์ด๋‹ค. MVC ์—์„œ ๋ชจ๋ธ๊ณผ ๋ทฐ์˜ ๊ฐ•ํ•œ ์˜์กด์„ฑ, MVP ์—์„œ ๋ทฐ์™€ ํ”„๋ ˆ์  ํ„ฐ์˜ ๊ฐ•ํ•œ ์˜์กด์„ฑ ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ•˜๊ธฐ ์œ„ํ•ด ๋“ฑ์žฅํ•œ ๋””์ž์ธ ํŒจํ„ด์œผ๋กœ Model ์€ ์–ดํ”Œ๋ฆฌ์ผ€์ด์…˜์—์„œ ์‚ฌ์šฉ๋˜๋Š” ๋ฐ์ดํ„ฐ์™€ ๊ทธ ๋ฐ์ดํ„ฐ๋ฅผ ์ฒ˜๋ฆฌํ•˜๋Š” ๋ถ€๋ถ„์ด๋‹ค. View ๋Š” ์‚ฌ์šฉ์ž์—๊ฒŒ ๋ณด์—ฌ์ง€๋Š” UI ๋ถ€๋ถ„์ด๊ณ  View Model ์€ View ๋ฅผ ์œ„ํ•œ View Model ๋กœ, View ๋ฅผ ํ‘œํ˜„ํ•˜๊ธฐ ์œ„ํ•œ Model ์„ ์ฒ˜๋ฆฌํ•˜๋Š” ๋ถ€๋ถ„์ด๋‹ค.

 

๊ตฌ๊ธ€์—์„œ๋Š” ์•„๋ž˜์™€ ๊ฐ™์€ ์•„ํ‚คํ…์ณ๋ฅผ ๊ถŒ๊ณ ํ•˜๊ณ  ์žˆ๋‹ค.

์ฃผ์˜๊นŠ๊ฒŒ ๋ด์•ผํ•˜๋Š” ๊ฒƒ์€ ํ™”์‚ดํ‘œ์˜ ๋ฐฉํ–ฅ ์ด๋‹ค. ๋ชจ๋“  ํ™”์‚ดํ‘œ๊ฐ€ ๋‹จ๋ฐฉํ–ฅ์œผ๋กœ ์—ฐ๊ฒฐ๋˜์–ด ์žˆ๋‹ค. ์•กํ‹ฐ๋น„ํ‹ฐ์™€ ํ”„๋ž˜๊ทธ๋จผํŠธ๋Š” ๋ทฐ๋ชจ๋ธ์˜ ์กด์žฌ๋ฅผ ์•Œ์ง€๋งŒ ๋ฐ˜๋Œ€๋กœ ๋ทฐ๋ชจ๋ธ์€ ์•กํ‹ฐ๋น„ํ‹ฐ์™€ ํ”„๋ž˜๊ทธ๋จผํŠธ์˜ ์กด์žฌ์— ๋Œ€ํ•ด ์•Œ์ง€ ๋ชปํ•œ๋‹ค. ๋ทฐ๋ชจ๋ธ์€ ๋ ˆํฌ์ง€ํ† ๋ฆฌ๋ฅผ ์ฐธ์กฐํ•˜๊ณ  ๋ ˆํฌ์ง€ํ† ๋ฆฌ๋Š” ๋ชจ๋ธ์„ ์ฐธ์กฐํ•œ๋‹ค. ๋ ˆํฌ์ง€ํ† ๋ฆฌ ์—ญ์‹œ ๋ทฐ๋ชจ๋ธ์˜ ์กด์žฌ๋ฅผ ์•Œ์ง€ ๋ชปํ•œ๋‹ค. ๊ตฌ๊ธ€ ๊ณต์‹ ์˜์ƒ - Build an App with Architecture Components (GDD India '17) ์—์„œ๋„ ๊ฐ ํด๋ž˜์Šค์˜ ํ•˜์œ„ ํด๋ž˜์Šค๋งŒ ์ฐธ์กฐํ•  ์ˆ˜ ์žˆ๋‹ค๊ณ  ์–ธ๊ธ‰ํ•˜๊ณ  ์žˆ๋‹ค. ์ฆ‰, ์•กํ‹ฐ๋น„ํ‹ฐ๊ฐ€ ๋ ˆํฌ์ง€ํ† ๋ฆฌ๋ฅผ ์ฐธ์กฐํ•ด์„œ๋Š” ์•ˆ๋œ๋‹ค๋Š” ์˜๋ฏธ๋‹ค.

 

๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด์„œ ์•„ํ‚คํ…์ณ ๊ตฌ์กฐ๊ฐ€ ์–ด๋–ป๊ฒŒ ๋ณ€ํ•˜๋Š”์ง€๋ฅผ ๋ณด๊ธฐ ์œ„ํ•ด ์•„๋ž˜์™€ ๊ฐ™์€ ์ˆœ์„œ๋กœ ์ฝ”๋“œ๋ฅผ ์ˆ˜์ •ํ•˜๋ฉด์„œ ์ง„ํ–‰ํ•˜๋„๋ก ํ•˜๊ฒ ๋‹ค.

 

ViewBinding
-> DataBinding
-> DataBinding + ViewModel
-> DataBinding + ViewModel + LiveData

1. ViewBinding

์ •๋ง ๊ธฐ๋ณธ์ ์ธ ๊ตฌํ˜„ ๋ฐฉ๋ฒ•์ด๋ผ๊ณ  ๋ณผ ์ˆ˜ ์žˆ๋‹ค. ์•กํ‹ฐ๋น„ํ‹ฐ ์— ๋ชจ๋“  ๋‚ด์šฉ์„ ๋•Œ๋ ค๋ฐ•๋Š”๋‹ค. findViewById ๋ฅผ ์‚ฌ์šฉํ•˜์ง€ ์•Š๊ณ  ViewBinding ์„ ์‚ฌ์šฉํ–ˆ๋Š”๋ฐ ViewBinding, ์ด ์นœ๊ตฌ ์ •๋ง ์ง„๊ตญ์ด๋‹ค. ํ•œ ๋ฒˆ ์‚ฌ์šฉํ•˜๋ฉด findViewById ๋Š” ๊ฑฐ๋“ค๋– ๋„ ์•ˆ๋ณด๊ฒŒ ๋œ๋‹ฌ๊นŒ?

Activity_Main.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World!"
        android:id="@+id/displayNumberTextView"
        android:layout_width="0dp"
        android:layout_height="100dp"
        android:layout_marginHorizontal="18dp"
        android:gravity="center"
        android:textSize="50sp"
        android:textStyle="bold"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        tools:text="20" />

    <com.google.android.material.floatingactionbutton.FloatingActionButton
        android:id="@+id/plusButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginEnd="18dp"
        android:src="@drawable/ic_baseline_add_24"
        app:layout_constraintBottom_toBottomOf="@+id/minusButton"
        app:layout_constraintEnd_toStartOf="@+id/minusButton" />

    <com.google.android.material.floatingactionbutton.FloatingActionButton
        android:id="@+id/minusButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginEnd="18dp"
        android:layout_marginBottom="18dp"
        android:src="@drawable/ic_baseline_remove_24"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

MainActivity.kt

increase ์™€ decrease ๋ฅผ ๊ตฌํ˜„ํ•˜๊ณ  setOnClickListener ์™€ ์—ฐ๊ฒฐํ–ˆ๋‹ค.

package com.example.basic_mvvm

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import com.example.basic_mvvm.databinding.ActivityMainBinding

class MainActivity : AppCompatActivity() {
    private val binding by lazy {
        ActivityMainBinding.inflate(layoutInflater)
    }

    private var num : Int = 0

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        setContentView(binding.root)

        bindViews()
        increase()
        decrease()
    }

    private fun bindViews() {
        binding.displayNumberTextView.text = num.toString()
    }

    private fun increase() {
        binding.plusButton.setOnClickListener {
            num += 1
            binding.displayNumberTextView.text = num.toString()
        }
    }

    private fun decrease() {
        binding.minusButton.setOnClickListener {
            num -= 1
            binding.displayNumberTextView.text = num.toString()
        }
    }
}

 

์ด๋ ‡๊ฒŒ ์ฝ”๋“œ๋ฅผ ์งœ๊ณ  ๋นŒ๋“œ๋ฅผ ์ง„ํ–‰ํ•ด๋ณด์ž.

์ •์ƒ์ ์œผ๋กœ ์•ฑ์ด ์ž‘๋™ํ•˜์ง€๋งŒ ์•กํ‹ฐ๋น„ํ‹ฐ๋ฅผ ์žฌ์‹คํ–‰(ํ™”๋ฉด ์ „ํ™˜)์ด ๋ฐœ์ƒํ•˜๋ฉด ๊ฐ’์ด ์ดˆ๊ธฐํ™”๋œ๋‹ค. ํ™”๋ฉด ์ „ํ™˜์ด ๋ฐœ์ƒํ•  ๋•Œ ๊ธฐ์กด ์•กํ‹ฐ๋น„ํ‹ฐ๊ฐ€ onDestory ๋˜๊ณ  ๋‹ค์‹œ onCreate ๋˜์–ด์„œ ๋ฐœ์ƒํ•˜๋Š” ๋ฌธ์ œ์ด๋‹ค. ๋ฌผ๋ก , savedInstance ๋ฅผ ํ™œ์šฉํ•˜๋ฉด ์ด๋ฅผ ํ•ด๊ฒฐํ•  ์ˆ˜ ์žˆ์ง€๋งŒ viewModel ์„ ์‚ฌ์šฉํ•  ๊ฒƒ์ด๋ฏ€๋กœ ์šฐ์„  ๋„˜์–ด๊ฐ€๋„๋ก ํ•˜์ž. ์•กํ‹ฐ๋น„ํ‹ฐ๋ฅผ ์žฌ์‹คํ–‰ํ•˜๋ฉด ๊ฐ’์ด ์ดˆ๊ธฐํ™”๋œ๋‹ค๋Š” ๊ฒƒ์— ์ดˆ์ ์„ ๋งž์ถ”์ž.

2. ViewBinding -> DataBinding

์•ž์„œ ViewBinding ์œผ๋กœ ๊ตฌํ˜„ํ•œ ๋ถ€๋ถ„์„ DataBinding ์œผ๋กœ ๋ณ€๊ฒฝํ•ด์คฌ๋‹ค. DataBindingUtil ์„ ์‚ฌ์šฉํ•œ๋‹ค. DataBinding ์€ ํ”„๋กœ๊ทธ๋ž˜๋งคํ‹ฑ ๋ฐฉ๋ฒ•์ด ์•„๋‹Œ ์„ ์–ธ์  ํ˜•์‹์œผ๋กœ ๋ ˆ์ด์•„์›ƒ์˜ UI ๊ตฌ์„ฑ์š”์†Œ ๋ฅผ ์•ฑ์˜ ๋ฐ์ดํ„ฐ ์†Œ์Šค์™€ ๊ฒฐํ•ฉํ•  ์ˆ˜ ์žˆ๊ฒŒ ํ•ด์ฃผ๋Š” ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ์ด๋‹ค. DataBinding ์˜ ์žฅ์ ์€ ๊ธฐ์กด์— ๋ทฐ์— ์ •์˜ํ–ˆ๋˜ setValue ๋‚˜ onClick ๋“ค์„ ๋ ˆ์ด์•„์›ƒ์— ๊ตฌํ˜„ํ•จ์œผ๋กœ์จ ๋ทฐ๊ฐ€ ์กฐ๊ธˆ ๋” ๊น”๋”ํ•ด์ง„๋‹ค.

// ๊ธฐ์กด ์ฝ”๋“œ  
binding.displayNumbertextView.text = num.toString()

// Databinding ์„ ์‚ฌ์šฉํ•œ ์ฝ”๋“œ
<TextView
    android:text="@{mainactivity.num}"/>

 

DataBinding ์„ ์‚ฌ์šฉํ•˜๊ธฐ ์œ„ํ•ด์„  XML ํŒŒ์ผ์„ ๋ณ€๊ฒฝํ•ด์ค˜์•ผ ํ•œ๋‹ค. ๊ธฐ์กด ๋ ˆ์ด์•„์›ƒ ์„ <layout> ์œผ๋กœ ๊ฐ์‹ธ์ค€๋‹ค. ์ดํ›„ <data> ์†์„ฑ์—์„œ ์–ด๋–ค ๋ณ€์ˆ˜๋ฅผ ๋ณด์—ฌ์ค„์ง€๋ฅผ ์ •์˜ํ•œ๋‹ค.

<layout 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">

    <data>
        <variable
            name="mainactivity"
            type="com.example.basic_mvvm.MainActivity" />
    </data>

    <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".MainActivity">

        <TextView
              android:text="@{mainactivity.num}"/>

        // ์ดํ•˜ ์ƒ๋žต

DataBinding ์„ ์‚ฌ์šฉํ•˜๋ฉด ViewBinding ์„ ์‚ฌ์šฉํ•  ๋•Œ๋ณด๋‹ค ์ข€ ๋” ๊ด€์‹ฌ์‚ฌ๋ฅผ ์ข€ ๋” ๋ถ„๋ฆฌ์‹œํ‚ฌ ์ˆ˜ ์žˆ๋‹ค. ๊ธฐ์กด์—๋Š” ๋ชจ๋“  ๋น„์ง€๋‹ˆ์Šค ๋กœ์ง์„ ์•กํ‹ฐ๋น„ํ‹ฐ์— ๋•Œ๋ ค ๋ฐ•๊ณ  ๋ทฐ์— ๋Œ€ํ•œ ๊ฐ„๋‹จํ•œ ์ฒ˜๋ฆฌ๋“ค(setValue, onClick)์„ ๋ทฐ์—์„œ ์ฒ˜๋ฆฌํ–ˆ์ง€๋งŒ XML ์—์„œ ์ฒ˜๋ฆฌํ•˜๋„๋ก ๋ณ€๊ฒฝํ•ด์คฌ๋‹ค.

 

์ด์ œ ๋นŒ๋“œ๋ฅผ ํ•ด๋ณด์ž! 

ํ•˜์ง€๋งŒ ์—ฌ์ „ํžˆ ๊ฐ’์ด ์ €์žฅ๋˜๊ณ  ์žˆ์ง€ ์•Š๋‹ค. ๋‹ค์Œ ๋‹จ๊ณ„๋กœ ๋„˜์–ด๊ฐ€๋ณด์ž.

3. DataBinding -> DataBinding + ViewModel

AAC ์˜ ๋ทฐ๋ชจ๋ธ ์ด ๋น›์„ ๋ฐœํ•  ์ฐจ๋ก€์ด๋‹ค. ์šฐ์„  ๋ทฐ๋ชจ๋ธ์€ ์•กํ‹ฐ๋น„ํ‹ฐ๋ณด๋‹ค ๊ธด ์ƒ๋ช… ์ฃผ๊ธฐ๋ฅผ ๊ฐ€์ง„๋‹ค.

MainActivity.kt

๋ทฐ๋ชจ๋ธ ์„ ์‚ฌ์šฉํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” ViewModelProviders ์„ ์‚ฌ์šฉํ•ด์•ผ ํ•œ๋‹ค. ViewModel ์˜ ์˜ค์šฐ๋„ˆ, LifecycleOwner ๋ฅผ ๋„ฃ์–ด์ค€๋‹ค. LifecycleOnwer์˜ ์ƒ๋ช… ์ฃผ๊ธฐ๋ฅผ ๋”ฐ๋ฅด๊ฒ ๋‹ค๋Š” ์˜๋ฏธ์ด๋‹ค. (Deprecated ๋˜์—ˆ๋‹ค... ViewModelProvider ๋ฅผ ์‚ฌ์šฉํ•˜๋„๋ก ํ•˜์ž)

package com.example.basic_mvvm
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import androidx.databinding.DataBindingUtil
import androidx.lifecycle.ViewModelProviders
import com.example.basic_mvvm.databinding.ActivityMainBinding
import com.example.basic_mvvm.viewmodel.MainViewModel

class MainActivity : AppCompatActivity() {
    private val binding by lazy {
        DataBindingUtil.setContentView<ActivityMainBinding>(this, R.layout.activity_main)
    }

    private val viewModel by lazy {
        ViewModelProviders.of(this).get(MainViewModel::class.java)
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        bindViews()
        increase()
        decrease()
    }

    private fun bindViews() {
        binding.displayNumberTextView.text = viewModel.getNumber().toString()
    }

    private fun increase() {
        binding.plusButton.setOnClickListener {
            viewModel.increase()
            binding.displayNumberTextView.text = viewModel.getNumber().toString()
        }
    }

    private fun decrease() {
        binding.minusButton.setOnClickListener {
            viewModel.decrease()
            binding.displayNumberTextView.text = viewModel.getNumber().toString()
        }
    }
}

ViewModel.kt

์ฝ”๋“œ๊ฐ€ ์ •๋ง ๋ถ€์‹คํ•˜์ง€๋งŒ num ์ฆ๊ฐ์— ๊ด€ํ•œ ๋น„์ฆˆ๋‹ˆ์Šค ๋กœ์ง์„ ์•กํ‹ฐ๋น„ํ‹ฐ ์—์„œ ๋ถ„๋ฆฌํ–ˆ๋‹ค.

package com.example.basic_mvvm.viewmodel

import androidx.lifecycle.ViewModel

class MainViewModel : ViewModel() {
    var num: Int = 0

    fun getNumber(): Int = num

    fun increase() = num++;

    fun decrease() = num--;
}

ํ™”๋ฉด์ด ๋ณ€๊ฒฝ๋˜์–ด๋„ ๊ฐ’์ด ์ดˆ๊ธฐํ™”๋˜์ง€ ์•Š๋Š” ๊ฒƒ์„ ๋ณผ ์ˆ˜ ์žˆ๋‹ค!

์ €์ž‘์žํ‘œ์‹œ๋น„์˜๋ฆฌ๋ณ€๊ฒฝ๊ธˆ์ง€

'๐Ÿ’ป ๊ฐœ๋ฐœ > Android' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

[Android] ์•Œ๋ฆผ ํด๋ฆญ์‹œ Activity, Fragment๋กœ ์ด๋™  (0) 2022.06.08
[Android] ๋ฐฑ๊ทธ๋ผ์šด๋“œ์—์„œ ์†Œ์ผ“ ํ†ต์‹ ์œผ๋กœ ์ด๋ฒคํŠธ ์ˆ˜์‹  ํ›„ ์•Œ๋ฆผ  (0) 2022.06.08
[Android] MVVM ํŒจํ„ด ์ ์šฉ๊ธฐ - 2  (0) 2022.05.14
[Android] MVVM ํŒจํ„ด ์ ์šฉ๊ธฐ - 1  (0) 2022.04.11
[Android] ์นด๋ฉ”๋ผ ๋˜๋Š” ๊ฐค๋Ÿฌ๋ฆฌ์—์„œ ์ด๋ฏธ์ง€ ๊ฐ€์ ธ์˜ค๊ธฐ  (0) 2022.02.06
[Android] ๋‹ค๊ตญ์–ด ์ง€์›  (0) 2022.01.13
    '๐Ÿ’ป ๊ฐœ๋ฐœ/Android' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€
    • [Android] ๋ฐฑ๊ทธ๋ผ์šด๋“œ์—์„œ ์†Œ์ผ“ ํ†ต์‹ ์œผ๋กœ ์ด๋ฒคํŠธ ์ˆ˜์‹  ํ›„ ์•Œ๋ฆผ
    • [Android] MVVM ํŒจํ„ด ์ ์šฉ๊ธฐ - 2
    • [Android] ์นด๋ฉ”๋ผ ๋˜๋Š” ๊ฐค๋Ÿฌ๋ฆฌ์—์„œ ์ด๋ฏธ์ง€ ๊ฐ€์ ธ์˜ค๊ธฐ
    • [Android] ๋‹ค๊ตญ์–ด ์ง€์›
    ๊ณ ๋„๊ณ ๋„
    ๊ณ ๋„๊ณ ๋„
    ์ข‹์•„ํ•˜๋Š” ๊ฒƒ์„ ํ•˜์ž\n ์Šค์œ„ํ”„ํŠธ ์ฐ๋จน์ค‘
    ๋Œ“๊ธ€์“ฐ๊ธฐ
    ๋‹ค์Œ ๊ธ€
    [Android] MVVM ํŒจํ„ด ์ ์šฉ๊ธฐ - 2
    ์ด์ „ ๊ธ€
    [Android] ์นด๋ฉ”๋ผ ๋˜๋Š” ๊ฐค๋Ÿฌ๋ฆฌ์—์„œ ์ด๋ฏธ์ง€ ๊ฐ€์ ธ์˜ค๊ธฐ
    • ์ด์ „
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • ๋‹ค์Œ