#1 ๊ฐ์ฒด ์ ๋ฌ์ ํ์์ฑ
#1-1 ์ด์ ๊ธ
Data Binding - ๊ธฐ์ด
#1 ๋ฐ์ดํฐ ๋ฐ์ธ๋ฉ ์ฌ์ฉ ์ #1-1 ์์ ์ฑ ์๊ณผ ๊ฐ์ ๊ฐ๋จํ ์ฑ์ด ์๋ค. Button์ ๋๋ฅด๋ฉด, EditText์ text๊ฐ ๋ฐ๋ก ์์ ์๋ TextView์ text์ ๋์ ๋๋ค. ์ด ์ฑ์ ์ฝ๋๋ ๋ค์๊ณผ ๊ฐ๋ค. #1-2 activity_main.xml #1-3 Main
kenel.tistory.com
์ด์ ๊ธ์์ ์ด์ด์ง๋ค. ์ด์ ๊ธ์์ , ๋ฐ์ดํฐ ๋ฐ์ธ๋ฉ์ ํตํด View์ ๋ ํผ๋ฐ์ค๋ฅผ ์ผ๊ด์ ์ผ๋ก ๊ฐ์ ธ์ ์ฐธ์กฐํ ์ ์์๋ค. ์ด๋ฒ์๋ ๋ฐ๋๋ก View์๊ฒ ๊ฐ์ฒด๋ฅผ ๋ณด๋ธ๋ค. ์๋์ ์์ ์ฑ์ ๋ณด์.
#1-2 ์์ ์ฑ
์ด ์ฑ์ ์ด๋ค ์ฑ
ํด๋์ค์ ๊ฐ์ฒด๋ฅผ ๋ฐ์ ํ๋ฉด์ ํ์ํ๋ ์ฑ์ด๋ค. ํ๋ฉด ์๋์ ์๋ ๋ฒํผ๋ค์ ๋๋ฅด๋ฉด ํ๋ฉด ์ ์ชฝ์ TextView๋ค์ text ์์ฑ์ ๋ณ๊ฒฝํ๋ค. ์ด์ ๊ธ์ ๋ด์ฉ์ ๋ฐ๋ผ ์ฝ๋๋ฅผ ์์ฑํ์ผ๋ฉฐ, ๊ทธ ์ฝ๋๋ ๋ค์๊ณผ ๊ฐ๋ค.
#1-3 activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<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">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
android:id="@+id/book_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="30sp"
app:layout_constraintBottom_toTopOf="@+id/book_author"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_chainStyle="packed" />
<TextView
android:id="@+id/book_author"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="30sp"
app:layout_constraintBottom_toTopOf="@id/book_year"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/book_name" />
<TextView
android:id="@+id/book_year"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="30sp"
app:layout_constraintBottom_toTopOf="@id/guideline"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/book_author" />
<androidx.constraintlayout.widget.Guideline
android:id="@+id/guideline"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
app:layout_constraintGuide_percent="0.5" />
<Button
android:id="@+id/button_book_1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="์๋ "
app:layout_constraintBottom_toTopOf="@id/button_book_3"
app:layout_constraintEnd_toStartOf="@id/button_book_2"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/guideline" />
<Button
android:id="@+id/button_book_2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="์ด๋ฆฐ ์์"
app:layout_constraintBottom_toTopOf="@id/button_book_4"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@+id/button_book_1"
app:layout_constraintTop_toBottomOf="@+id/guideline" />
<Button
android:id="@+id/button_book_3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="๋์ฟ์ ๋ํ
์ผ"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="@+id/button_book_4"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/button_book_1" />
<Button
android:id="@+id/button_book_4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="ํ๋ฆฝ ์ฑํน"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@id/button_book_3"
app:layout_constraintTop_toBottomOf="@+id/button_book_2" />
</androidx.constraintlayout.widget.ConstraintLayout>
</layout>
#1-4 Book.kt
// package com.example.withobjectpractice
data class Book (
val name: String,
val author: String,
val year: Int
)
#1-5 MainActivity.kt
// package com.example.withobjectpractice
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.Button
import androidx.databinding.DataBindingUtil
import com.example.withobjectpractice.databinding.ActivityMainBinding
class MainActivity : AppCompatActivity() {
private lateinit var binding : ActivityMainBinding
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = DataBindingUtil.setContentView(this, R.layout.activity_main)
onShowBookInfoButtonClick(binding.buttonBook1, getWalden())
onShowBookInfoButtonClick(binding.buttonBook2, getTheLittlePrince())
onShowBookInfoButtonClick(binding.buttonBook3, getDetailsOfTokyo())
onShowBookInfoButtonClick(binding.buttonBook4, getFlipThinking())
}
private fun onShowBookInfoButtonClick(button: Button, book: Book) {
button.setOnClickListener{
binding.bookName.text = "์ด๋ฆ: " + book.name
binding.bookAuthor.text = "์ ์: " + book.author
binding.bookYear.text = "์ถํ๋
๋: " + book.year.toString() + "๋
"
}
}
private fun getWalden(): Book {
return Book("์๋ ", "David Thoreau", 1854)
}
private fun getTheLittlePrince(): Book {
return Book("์ด๋ฆฐ ์์", "Saint-Exupéry", 1943)
}
private fun getDetailsOfTokyo(): Book {
return Book("๋์ฟ์ ๋ํ
์ผ", "์๊ฐ๋
ธํธ", 2018)
}
private fun getFlipThinking(): Book {
return Book("ํ๋ฆฝ ์ฑํน", "Berthold Gunster", 2023)
}
}
์ฑ
๊ฐ์ฒด๋ฅผ ๋ฐ์์ ์ด๋ฆ, ์ ์, ์ถํ๋
๋๋ก ์ด๋ฃจ์ด์ง ํ๋กํผํฐ๋ฅผ ํ๋ํ๋ View์ ๋์
ํ๋ค. ์ด ์ฝ๋๋ findViewById()๋ฅผ ์ฌ์ฉํ๋ ๊ฒ๋ณด๋ค๋ ๋ซ๋ค. ํ์ง๋ง, ์ฑ์ ๊ธฐ๋ฅ์ด ๋ง์์ง๊ณ Activity์ ์ฝ๋ ๊ธธ์ด๋ ๊ธธ์ด์ง๋ฉด, ์ด์ ๊ฐ์ด View์ ์ธ๋ถ ์ฌํญ์ ๊ด์ฌํ๋ ์ฝ๋๋ ๊ต์ฅํ ๊ฑฐ์ฌ๋ฆฌ๊ฒ ๋ ๊ฒ์ด๋ค. ์ด ๋, ์ธ๋ถ ์ฌํญ์ Activity์์ ์ฒ๋ฆฌํ์ง ์๊ณ , Book ๊ฐ์ฒด ์์ฒด๋ฅผ View์ ์ ๋ฌํด์ ๊ทธ View๋ณด๊ณ ์์์ ๊ทธ ๊ฐ์ฒด๋ฅผ ๋ค๋ฃจ๋ผ๊ณ ํ๋ค๋ฉด Activity(Cotroller)๋ ๋ ๊ฐ๋ฒผ์์ง ๊ฒ์ด๋ค. ์ฆ ๊ฐ์ฒด๋ฅผ ์ ๋ฌํ๋ค๋ ๋ง์, Cotroller๋ ๋ฐ์ดํฐ๋ง ์ ๋ฌํ๊ณ ๊ทธ ๋ฐ์ดํฐ๊ฐ ์ด๋ป๊ฒ ๋ณด์ผ์ง๋ ๊ด์ฌํ์ง ์๊ฒ ๋ง๋ค๊ฒ ๋ค๋ ์๋ฆฌ๋ค.
์ง๊ธ๋ถํฐ #1์ ์ฝ๋๊ฐ ๊ฐ์ฒด๋ฅผ ์ ๋ฌํ๊ฒ๋ ๋ฆฌํฉํ ๋ง ํด๋ณธ๋ค.
#2 ๊ฐ์ฒด๋ฅผ View์ ๋ณด๋ด๊ฒ ์์ ํ๊ธฐ
#2-1 activity_main.xml์์ <variable> ํ๊ทธ ์ถ๊ฐ ๋ฐ ํ์ฉ
<?xml version="1.0" encoding="utf-8"?>
<layout ... >
<data>
<variable
name="book"
type="com.example.withobjectpractice.Book" />
</data>
<androidx.constraintlayout.widget.ConstraintLayout ... >
<TextView
...
android:text="@{`์ด๋ฆ: ` + book.name}"
... />
<TextView
...
android:text="@{`์ ์: ` + book.author}"
... />
<TextView
...
android:text="@{`์ถํ๋
๋: ` + book.author + `๋
`}"
... />
...
</androidx.constraintlayout.widget.ConstraintLayout>
</layout>
<data> ํ๊ทธ๋ฅผ xml์ ๋ฃ๋๋ค. ๊ทธ ์์ <variable> ํ๊ทธ๋ฅผ ๋ฃ๋๋ค. variable ํ๊ทธ๋ type๊ณผ name์ 2๊ฐ์ง ์์ฑ์ด ์๋ค. type์๋ data class์ ์ ํํ ์์น๋ฅผ ์ ๊ณ , name์๋ type์ ๊ธด ์ด๋ฆ์ ๋์ ํ ๋ณ๋ช
์ ์ง์ ํ๋ค. ์ด์ Data binding ํด๋์ค๊ฐ ์๋์ผ๋ก, ActivityMainBinding ํด๋์ค์ book์ด๋ผ๋ ์ด๋ฆ์ ํ๋กํผํฐ๋ฅผ ์ถ๊ฐํ๋ค. ActivityMainBinding ํด๋์ค์ ํ์ฉ์ MainActivity.kt์ ๋ชซ์ด๋ค. MainActivity.kt๊ฐ ์ด๋ค Book ๊ฐ์ฒด๋ฅผ View๋จ์ผ๋ก ๋ณด๋ด์ค ์ง๋ View ์
์ฅ์์ ์ ๊ธธ์ด ์์ผ๋, ๊ทธ๊ฒ์ด ์ด๋ป๊ฒ ๋ณด์ผ์ง๋ ํต์ ํ ์ ์๋ค. ์๋ฅผ ๋ค์ด, <TextView>์ ์์ฑ android:text์ "@{book.author}"์ ๊ฐ์ด book ํ๋กํผํฐ๋ฅผ ์ฐธ์กฐํ๋ ๊ฐ์ ๋ฃ๋ ๊ฒ๊ณผ ๊ฐ์ด ๋ง์ด๋ค.
#2-2 MainActivity.kt ์์
// package com.example.withobjectpractice
...
class MainActivity : AppCompatActivity() {
...
private fun onShowBookInfoButtonClick(button: Button, book: Book) {
button.setOnClickListener{
binding.book = book
}
}
...
}
ActivityMainBinding์ ํ๋กํผํฐ์ book์ด ์ถ๊ฐ๋์์ผ๋ฏ๋ก, ํด๋น ํ๋กํผํฐ๋ฅผ ํ์ฉํ๊ฒ๋ ์ฝ๋๋ฅผ ์์ ํ๋ค. ์ดํ์ ์์
์ฆ ์ด ๊ฐ์ฒด๋ฅผ ์ด๋ป๊ฒ ์ ํ์ํ ๊ฒ์ธ๊ฐ?๋ activity_main.xml (View)์๊ฒ ๋งก๊ธด๋ค. (#2-1 ์ฐธ์กฐ)
#2-3 ๋ณ๊ฒฝ์ฌํญ ์๋ ํ์ธ, ์์ฌ์ด ์
์ ๋๋ก ์๋ํ๋ค. ํ์ง๋ง, ์๋ฌด ๋ฒํผ๋ ํด๋ฆญ๋์ง ์์ ์ฒ์ ํ๋ฉด์ด ๊ฑฐ์ฌ๋ฆฐ๋ค. <variable>์ ์๋ฌด๋ฐ ๊ฐ์ฒด๊ฐ ์ ๋ฌ๋์ง ์์๊ธฐ ๋๋ฌธ์, null์ ๋ฐํํ๋ ๊ฒ์ด๋ค. ๊ฐ์ฒด๊ฐ ์ ๋ฌ๋์ง ์์ ์ํฉ์์๋ ์์ฐ์ค๋ฝ๊ฒ ํ๋ฉด์ ํ์ํ๊ธฐ ์ํด์ ๋ค์๊ณผ ๊ฐ์ด ์ฝ๋๋ฅผ ๋ณ๊ฒฝํ๋ค.
#2-4 null๊ฐ์ ๊ณ ๋ คํ XML ์ฝ๋
<?xml version="1.0" encoding="utf-8"?>
<layout ... >
<data>
<variable ... >
</data>
<androidx.constraintlayout.widget.ConstraintLayout ... >
<TextView
...
android:text="@{book != null ? `์ด๋ฆ: ` + book.name : `์ฑ
์ ๋ณด ์์`}"
... />
<TextView
...
android:text="@{book != null ? `์ ์: ` + book.author : ``}"
... />
<TextView
...
android:text="@{book != null ? `์ถํ๋
๋: ` + book.year + `๋
` : ``}"
... />
...
</androidx.constraintlayout.widget.ConstraintLayout>
</layout>
book์ ๋ํด null ์ฒดํฌ๋ฅผ ํ๊ณ , ๊ฐ์ฒด๊ฐ null์ผ ๋์ ์๋ ๋๋ฅผ ๋ถ๊ธฐ์์ผ ๊ฐ๊ฐ ํ์ํ ๋ฌธ์์ด์ ์จ ๋ฃ๋๋ค. ์ด๋ ๊ฒ ๋ณ๊ฒฝํ๊ณ ์ฑ์ ์คํ์ํค๋ฉด ๋ค์๊ณผ ๊ฐ๋ค.
#2-5 ๊ฒฐ๋ก (๊ฐ์ฒด ์ ๋ฌ์ ์ด์ )
๋ฐ์ดํฐ ๋ฐ์ธ๋ฉ ๋ผ์ด๋ธ๋ฌ๋ฆฌ๋ฅผ ํตํด์, data class ๋ฑ์ ๊ฐ์ฒด(Model)๋ฅผ View์ ์ง์ (directly) ๋ณด๋ผ ์ ์๋ค. ์ด ๋ฐฉ์์ผ๋ก Activity์ View(xml) ๊ฐ์ ๊ฒฐํฉ๋๋ฅผ ๋์จํ๊ฒ ๋ง๋ค์๋ค. Activity๋์ View์ ์ด๋ค ๋ฐ์ดํฐ๋ฅผ ๋ณด๋ผ์ง๋ง ๊ณ ๋ฏผํ๊ณ , ๊ทธ ์ด์์ผ๋ก ์ ๊ฒฝ์ฐ์ง ์๋๋ค. View๋ Activity๋ก๋ถํฐ ๋ฐ์๋ค์ธ ๊ฐ์ฒด๋ฅผ ์ค๋ก์ง ์ด๋ป๊ฒ ํ์ํ ์ง๋ง์ ๊ธฐ์ ํ๋ค. ์ด๋ฌํ ๋์จํ ๊ฒฐํฉ๋๋ ๋ฐ์ดํฐ ๋ฐ์ธ๋ฉ ์ฌ์ฉ์ผ๋ก ์ป์ ์ ์๋ ํฐ ์ด์ ์ด๋ค.
#3 ์์ฝ
๊ฐ์ฒด๋ฅผ View์ ๋ณด๋ด๋ ์ด์ ๋ '๋ถ์ '์ ํ๊ธฐ ์ํจ์ด๋ค.
#4 ์์ฑ๋ ์ฑ
android-practice/data-binding/ObjectToView at master · Kanmanemone/android-practice
Contribute to Kanmanemone/android-practice development by creating an account on GitHub.
github.com
'๊นจ์ ๊ฐ๋ ๐ > Android' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[Android] LiveData - ๊ธฐ์ด (0) | 2024.01.16 |
---|---|
[Android] ViewModel - ๋ทฐ ๋ชจ๋ธ์ ์ธ์(Argument) ์ ๋ฌ (ViewModelFactory) (0) | 2024.01.15 |
[Android] ViewModel - ๊ธฐ์ด (0) | 2024.01.13 |
[Android] Data Binding - ๊ธฐ์ด (0) | 2024.01.11 |
AndroidX (๊ตฌ Support Library) (0) | 2023.12.13 |