image

๐ŸŽฏ DataBinding

  • xmlํŒŒ์ผ์— Data๋ฅผ ์—ฐ๊ฒฐํ•ด์„œ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๊ฒŒ ๋„์™€์ฃผ๋Š” Android JetPack ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์˜ ํ•˜๋‚˜์˜ ๊ธฐ๋Šฅ
  • ์–‘๋ฐฉํ–ฅ ๋ฐ์ดํ„ฐ ๊ฒฐํ•ฉ์„ ์ง€์›

    ์–‘๋ฐฉํ–ฅ ๋ฐ์ดํ„ฐ ๊ฒฐํ•ฉ

    1. When properties in the model get updated, so does the UI.
      • model ์ •๋ณด๊ฐ€ ๋ณ€๊ฒฝ๋˜์—ˆ์„ ๋•Œ ui๋„ ๊ฐ™์ด ๋ณ€๊ฒฝ๋˜๋Š” ์ž‘์—…
    2. When UI elements get updated, the changes get propagated back to the model.
      • ui ๋ฐ์ดํ„ฐ๊ฐ€ ๋ณ€๊ฒฝ๋˜์—ˆ์„ ๋•Œ ๊ทธ ๋‚ด์šฉ์ด model์— ์ ์šฉ๋˜๋Š” ์ž‘์—…

ํŠน์ง•

  • Databinding์„ ์‚ฌ์šฉํ•˜๋ฉด findViewById(), ๋ฒ„ํ„ฐ๋‚˜์ดํ”„๋ฅผ ์“ฐ์ง€ ์•Š์•„๋„ xml์— ๋งŒ๋“  View๋“ค์„ ์ž๋™์œผ๋กœ ๋งŒ๋“ค์–ด ์ค€๋‹ค.
  • Data๊ฐ€ ๋ฐ”๋€Œ๋ฉด ์•Œ์•„์„œ ๋ฐ”๋€ Data๋กœ View๋ฅผ ๋ณ€๊ฒฝํ•˜๊ฒŒ ํ• ์ˆ˜๋„ ์žˆ๋‹ค. (์˜ต์ €๋ธ” ์‚ฌ์šฉ์‹œ)
  • RecyclerView์—์„œ ๊ฐ๊ฐ์˜ item์„ ์„ธํŒ… ํ•ด์ฃผ๋Š” ์ž‘์—…๋„ xml์—์„œ ๋‹ค ์จ์ฃผ๋ฉด ์•Œ์•„์„œ ์ฒ™์ฒ™ ๊ฐ’์ด ๋“ค์–ด๊ฐ„๋‹ค.
  • ๊ตฌ๊ธ€์˜ ๊ถŒ์žฅ์‚ฌํ•ญ

์‚ฌ์šฉ

Gradle ์„ค์ •

  • android 4.0 ์ด์ƒ
    android {
      ...
      buildFeatures {
          dataBinding = true
      }
    }
  • android 3.6 ~ 4.0
    android {
      ...
      dataBinding {
          enabled = true
      }
    }

MainActivity

class MainActivity : AppCompatActivity() {

    private lateinit var binding: ActivityMainBinding

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = DataBindingUtil.setContentView(this, R.layout.activity_main)

        binding.user = UserProfile("์ง€์€", "์ด")
    }
}
  • infalte
    • ์ƒ์„ฑ๋œ binding ๊ฐ์ฒด๋ฅผ ์–ป์„ ์ˆ˜ ์žˆ๋Š” ๋ฉ”์†Œ๋“œ
  • binding.root
    • ํ˜„์žฌ viewBinding ๊ฐ์ฒด์—์„œ ๊ฐ€์žฅ ๊ธฐ๋ณธ์ด ๋˜๋Š” layout์„ ๋ฐ˜ํ™˜

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">
    <data>
        <variable
            name="user"
            type="com.example.temp.UserProfile" />
    </data>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        tools:context=".MainActivity">

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="@{user.firstName}" />

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="@{user.lastName}" />
    </LinearLayout>
</layout>
  • dataBinding์€ ๊ฐ€์žฅ ๋ฐ”๊นฅ์— <layout>ํƒœ๊ทธ๋กœ ๊ฐ์‹ธ์ฃผ์–ด์•ผ ํ•˜๊ณ  <data>๋ฅผ ํ†ตํ•ด view์— ๋ณด์—ฌ์งˆ ๊ฐ’์„ ์„ ์–ธํ•œ๋‹ค.
  • view์—์„œ๋Š” @{}๋ฅผ ํ†ตํ•ด ๋ฐ์ดํ„ฐ๋ฅผ ํ™”๋ฉด์— ๋ฟŒ๋ฆฐ๋‹ค.
    • @{}๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์ด ๊ฐ„ํŽธ์‹์œผ๋กœ๋„ ์ ์šฉ์ด ๊ฐ€๋Šฅํ•˜๋‹ค.
      • @{(userProfile.gender == UserProfile.GENDER_MALE) ? "๋‚จ์„ฑ" : "์—ฌ์„ฑ"}

๐ŸŽฏ ViewBinding

  • view์™€ ์ƒํ˜ธ ์ž‘์šฉํ•˜๋Š” ์ฝ”๋“œ๋ฅผ ๋”์šฑ ๊ฐ„ํŽธํ•˜๊ฒŒ ํ•  ์ˆ˜ ์žˆ๋„๋ก ํ•˜๋Š” ๊ธฐ๋Šฅ
  • ์ด๋ฅผ ํ™œ์šฉํ•˜์—ฌ findViewById๋ฅผ ๋Œ€์ฒดํ•  ์ˆ˜ ์žˆ๋‹ค.
  • ๋งŽ์ด ์‚ฌ์šฉํ•˜๋Š” DataBinding์—์„œ ๋งŽ์€ ๊ฐœ๋ฐœ์ž๋“ค์ด ๋‹จ์ˆœํžˆ View์— ๋Œ€ํ•œ ์ฐธ์กฐ๋ฅผ ์–ป์–ด์„œ ์‚ฌ์šฉํ–ˆ์Œ
    • ์ด๋ฅผ ๋ณธ ๊ตฌ๊ธ€์ด View์— ๋Œ€ํ•œ ์ฐธ์กฐ ๋ชฉ์ ์œผ๋กœ ViewBinding์„ ์ง€์›ํ•˜๊ธฐ ์‹œ์ž‘ํ•จ

ํŠน์ง•

  • Null ์•ˆ์ „
    • ๋ทฐ์˜ ์ง์ ‘ ์ฐธ์กฐ๋ฅผ ์ƒ์„ฑํ•˜๋ฏ€๋กœ ์œ ํšจํ•˜์ง€ ์•Š์€ ๋ทฐ ID๋กœ ์ธํ•ด null ํฌ์ธํ„ฐ ์˜ˆ์™ธ๊ฐ€ ๋ฐœ์ƒํ•  ์œ„ํ—˜์ด ์—†์Œ
    • ๋ ˆ์ด์•„์›ƒ์˜ ์ผ๋ถ€ ๊ตฌ์„ฑ์—๋งŒ ๋ทฐ๊ฐ€ ์žˆ๋Š” ๊ฒฝ์šฐ ๊ฒฐํ•ฉ ํด๋ž˜์Šค์—์„œ ์ฐธ์กฐ๋ฅผ ํฌํ•จํ•˜๋Š” ํ•„๋“œ๊ฐ€ @Nullable๋กœ ํ‘œ์‹œ
  • ์œ ํ˜• ์•ˆ์ „
    • ๊ฐ ๋ฐ”์ธ๋”ฉ ํด๋ž˜์Šค์— ์žˆ๋Š” ํ•„๋“œ์˜ ์œ ํ˜•์ด XML ํŒŒ์ผ์—์„œ ์ฐธ์กฐํ•˜๋Š” ๋ทฐ์™€ ์ผ์น˜ํ•˜๊ธฐ ๋•Œ๋ฌธ์— ํด๋ž˜์Šค ๋ณ€ํ™˜ ์˜ˆ์™ธ๊ฐ€ ๋ฐœ์ƒํ•  ์œ„ํ—˜์ด ์—†๋‹ค.
  • ๋น ๋ฅธ ์ปดํŒŒ์ผ ์†๋„์™€ ๋”ฐ๋กœ xml ํŒŒ์ผ์— ํƒœ๊ทธ๊ฐ€ ํ•„์š”ํ•˜์ง€ ์•Š๊ณ  ์ž๋™์œผ๋กœ ์ ์šฉ๋˜๋ฏ€๋กœ ์‚ฌ์šฉ ํŽธ์˜์„ฑ์ด ์žˆ๋‹ค.
  • Data Binding๊ณผ ๋น„๊ตํ–ˆ์„ ๋•Œ ๋ ˆ์ด์•„์›ƒ ๋ณ€์ˆ˜ ๋˜๋Š” ๋ ˆ์ด์•„์›ƒ ํ‘œํ˜„์‹์„ ์ง€์›ํ•˜์ง€ ์•Š์œผ๋ฏ€๋กœ XML ๋ ˆ์ด์•„์›ƒ ํŒŒ์ผ์—์„œ ์ง์ ‘ ๋™์  UI ์ฝ˜ํ…์ธ ๋ฅผ ์„ ์–ธํ•˜๋Š” ๋ฐ ์‚ฌ์šฉํ•  ์ˆ˜ ์—†์Œ.
  • ์–‘๋ฐฉํ–ฅ ๋ฐ์ดํ„ฐ ๊ฒฐํ•ฉ์„ ์ง€์›ํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค.
    • DataBinding์—์„œ๋Š” ์ฝ”๋“œ์—์„œ ๋ฐ์ดํ„ฐ๋ฅผ ๋ฐ›์•„์™€ xml์— ์ ์šฉ์ด ๊ฐ€๋Šฅํ–ˆ์œผ๋‚˜ ViewBinding์—์„œ๋Š” ๋ถˆ๊ฐ€ํ•จ

์‚ฌ์šฉ

Gradle ์„ค์ •

  • android 4.0 ์ด์ƒ
    android {
      ...
      buildFeatures {
          viewBinding = true
      }
    }
  • android 3.6 ~ 4.0
    android {
      ...
      viewBinding {
          enabled = true
      }
    }

MainActivity

class MainActivity : AppCompatActivity() { 
    private lateinit var binding: ActivityMainBinding

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = ActivityMainBinding.inflate(layoutInflater)

        setContentView(binding.root)

        binding.txtSample.text = "View Binding Sample"
    }
}
  • infalte
    • ์ƒ์„ฑ๋œ binding ๊ฐ์ฒด๋ฅผ ์–ป์„ ์ˆ˜ ์žˆ๋Š” ๋ฉ”์†Œ๋“œ
  • binding.root
    • ํ˜„์žฌ viewBinding ๊ฐ์ฒด์—์„œ ๊ฐ€์žฅ ๊ธฐ๋ณธ์ด ๋˜๋Š” layout์„ ๋ฐ˜ํ™˜

activity_main.xml

<LinearLayout ... >
    <TextView android:id="@+id/txt_sample" />
</LinearLayout>

dataBinding๊ณผ viewBinding์˜ ์ฐจ์ด

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

'Android > Android ๊ธฐ์ดˆ' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

Json & Gson  (0) 2022.04.18
Android: Fragment  (0) 2022.03.23
Android: Data Class  (0) 2022.03.23
Android: Intent  (0) 2022.03.23
Android: Activity  (0) 2022.03.23
  • ๋„ค์ด๋ฒ„ ๋ธ”๋Ÿฌ๊ทธ ๊ณต์œ ํ•˜๊ธฐ
  • ๋„ค์ด๋ฒ„ ๋ฐด๋“œ์— ๊ณต์œ ํ•˜๊ธฐ
  • ํŽ˜์ด์Šค๋ถ ๊ณต์œ ํ•˜๊ธฐ
  • ์นด์นด์˜ค์Šคํ† ๋ฆฌ ๊ณต์œ ํ•˜๊ธฐ