[Android] ViewBinding ์ •๋ฆฌ

Minjun Kimยท2023๋…„ 8์›” 30์ผ
0

Android

๋ชฉ๋ก ๋ณด๊ธฐ
28/47
post-thumbnail

๐Ÿ“ SeSAC์˜ 'JetPack๊ณผ Kotlin์„ ํ™œ์šฉํ•œ Android App ๊ฐœ๋ฐœ' ๊ฐ•์ขŒ๋ฅผ ์ •๋ฆฌํ•œ ๊ธ€ ์ž…๋‹ˆ๋‹ค.


๐Ÿ“š View ํ™œ์šฉ ๊ธฐ๋ฒ•

  • findViewById()
  • ButterKnife
  • Kotlin Android Extension
  • DataBinding
  • ViewBinding
- findViewById()

๊ฐ€์žฅ ๊ธฐ์ดˆ์ ์ธ ๋ฐฉ๋ฒ•์ด์ž ๊ฐ€์žฅ ๋‹จ์ˆœํ•˜๋ฉด์„œ ์ฝ”๋“œ๊ฐ€ ๊ธด ๋ฐฉ๋ฒ•
- ButterKnife

์ƒ๋‹นํžˆ ์œ ๋ช…'ํ–ˆ๋˜' ํ”„๋ ˆ์ž„์›
- Kotlin Android Extension

์•ˆ๋“œ๋กœ์ด๋“œ์—์„œ ์ฝ”ํ‹€๋ฆฐ์„ ๊ณต์‹์ ์œผ๋กœ ์ง€์›ํ•˜๋ฉด์„œ ํ•œ๋™์•ˆ ์“ฐ์˜€๋˜ ๊ธฐ๋ฒ•
๋ทฐ๋ฐ”์ธ๋”ฉ์˜ ๋“ฑ์žฅ๊ณผ ๋™์‹œ์— deprecated ๋‹นํ–ˆ๋‹ค.
- DataBinding

๊ตฌ๊ธ€์—์„œ ์ œ์‹œํ•˜๊ณ  ์žˆ๋Š” ๊ธฐ๋ฒ•
์ฐธ๊ณ ๋กœ ๋ฐ์ดํ„ฐ ๋ฐ”์ธ๋”ฉ๋„ Kotlin Android Extension ์ฒ˜๋Ÿผ ์œ ๊ธฐ๊ฐ€ ์œ ๋ ฅํ•˜๋‹ค...


- ViewBinding

๊ตฌ๊ธ€์—์„œ ์ œ์‹œํ•˜๊ณ  ์žˆ๋Š” ๊ธฐ๋ฒ•
๊ฐ€์žฅ ์ตœ์‹  ๊ธฐ๋ฒ•์ด๋‹ค. ๋ทฐ๋ฐ”์ธ๋”ฉ์˜ ๋“ฑ์žฅ๊ณผ ํ•จ๊ป˜ Kotlin Android Extension ๊ฐ€ deprecated ๋๋‹ค.

๐Ÿ“š ViewBinding

๐Ÿ“Œ build.gradle ์— ์š”์†Œ ์„ ์–ธ

android {
	viewBinding {
    	enabled = true
	}
}
  • XML ํŒŒ์ผ์— ๋“ฑ๋ก๋œ ๋ทฐ ๊ฐ์ฒด๋ฅผ ๊ฐ€์ง€๋Š” Binding ํด๋ž˜์Šค๊ฐ€ generate

  • activity_main.xml -> ActivityMainBinding

๐Ÿ“Œ ์ฝ”๋“œ์— ๋ฐ”์ธ๋”ฉ ์„ ์–ธ

val binding = ActivityMainBinding.inflate(layoutInflater)
// ์•กํ‹ฐ๋น„ํ‹ฐ ํ™”๋ฉด ์ถœ๋ ฅ
setContentView(binding.root)

// ๋ทฐ ๊ฐ์ฒด ์ด์šฉ
binding.visibleBtn.setOnClickListener {
	binding.targetView.visible = View.VISIBLE
}
binding.invisibleBtn.setOnClickListener {
	binding.targetView.visible = View.INVISIBLE
}
  • Binding ํด๋ž˜์Šค์˜ inflate ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•˜๋ฉด์„œ ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ layoutInflator ๋ฅผ ์ „๋‹ฌํ•˜์—ฌ ๊ฐ์ฒด ํš๋“

  • Binding ๊ฐ์ฒด์˜ root ํ”„๋กœํผํ‹ฐ์—๋Š” XML ์˜ ๋ฃจํŠธ ํƒœ๊ทธ ๊ฐ์ฒด

๐Ÿ“Œ ๋ฐ”์ธ๋”ฉ ์˜ˆ์™ธ ์ฒ˜๋ฆฌ

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    ..............
    tools:viewBindingIgnore="true">
  • build.gradle ํŒŒ์ผ์— ViewBinding ์ด์šฉํ•˜๊ฒ ๋‹ค๊ณ  ์„ ์–ธ์„ ํ•˜๋ฉด XML ํ•˜๋‚˜ ๋‹น Binding ํด๋ž˜์Šค๊ฐ€ ์ž๋™์œผ๋กœ generate

  • XML ํŒŒ์ผ์„ ์œ„ํ•œ Binding ํด๋ž˜์Šค๊ฐ€ ๋งŒ๋“ค์–ด์งˆ ํ•„์š”๊ฐ€ ์—†๋Š” ๊ฒฝ์šฐ

  • tools:viewBindingIgnore="true" ๋กœ ์„ ์–ธ


๐Ÿงฉ ์‹ค์Šต ์˜ˆ์ œ

  • activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity">

    <Button
        android:id="@+id/visibleBtn"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="visible" />

    <TextView
        android:id="@+id/targetView"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:background="#FF0000"
        android:gravity="center"
        android:text="hello world"
        android:textColor="#FFFFFF"
        android:textSize="30dp" />

    <Button
        android:id="@+id/invisibleBtn"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="invisible" />

</LinearLayout>

  • MainActivity.kt
package com.kotdev99.android.c90

class MainActivity : AppCompatActivity() {

	lateinit var binding: ActivityMainBinding

	override fun onCreate(savedInstanceState: Bundle?) {
		super.onCreate(savedInstanceState)

		binding = ActivityMainBinding.inflate(layoutInflater)
		setContentView(binding.root)

		binding.visibleBtn.setOnClickListener {
			binding.targetView.visibility = View.VISIBLE
		}
		binding.invisibleBtn.setOnClickListener {
			binding.targetView.visibility = View.INVISIBLE
		}
	}
}

๐Ÿ“ฒ ๊ฒฐ๊ณผ

profile
์‘์•  ๋‚˜ ์•„๊ธฐ ๋‰ด๋น„

0๊ฐœ์˜ ๋Œ“๊ธ€