๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
๐Ÿš€ Development/Android

[์•ˆ๋“œ๋กœ์ด๋“œ Android] Data Binding ์‚ฌ์šฉ๋ฒ•

by Jay Din 2023. 7. 17.
728x90
๋ฐ˜์‘ํ˜•

DataBinding๊ณผ ViewBinding์ด ๋ญ๊ฐ€ ๋‹ค๋ฅธ๊ฐ€?

๊ฐ„๋‹จํ•˜๊ฒŒ ๋งํ•˜์ž๋ฉด, DataBinding์€ ์ด๋ฆ„์ฒ˜๋Ÿผ ๋ฐ์ดํ„ฐ๋ฅผ ์—ฐ๊ฒฐํ•ด์ฃผ๋Š” ์—ญํ• ์„ ํ•  ์ˆ˜ ์žˆ๋‹ค.

(๋ฐ์ดํ„ฐ์™€ ๊ฐ™์ด ๊ฒฐํ•ฉํ•ด์„œ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Œ)

 

// ์ž์„ธํ•œ ์ด๋ก ์€ ViewBinding vs DataBinding ์ฐธ๊ณ 

 

์„ธํŒ…

build.gradle

android {
	.
	..
    // ์ถ”๊ฐ€
    buildFeatures{
        dataBinding = true
    }
}
๋ฐ˜์‘ํ˜•

Activity

xml

<layout></layout> ์ „์ฒด๋ฅผ ๊ฐ์‹ผ๋‹ค.

<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools">

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

        <TextView
            android:id="@+id/dataBindingTextView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Hello World!"
            android:textSize="50sp" />

    </LinearLayout>

</layout>

MainActivity.kt

class MainActivity : AppCompatActivity() {

    // 1. ์ถ”๊ฐ€
    private lateinit var binding : ActivityMainBinding

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        // 2. ์ถ”๊ฐ€
        binding = DataBindingUtil.setContentView(this, R.layout.activity_main)

        // ์˜ˆ์ œ ์ฝ”๋“œ
        binding.dataBindingTextView.text = "๋ฐ”๋€ ํ…์ŠคํŠธ"

    }
}

Fragment

XML

<layout></layout>์œผ๋กœ ์ „์ฒด ๋ ˆ์ด์•„์›ƒ์„ ๊ฐ์‹ผ๋‹ค.

<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools">
    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".TestFragment">

        <!-- TODO: Update blank fragment layout -->
        <TextView
            android:id="@+id/fragmentText"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:text="@string/hello_blank_fragment"
            android:textSize="50sp" />

    </FrameLayout>
</layout>
๋ฐ˜์‘ํ˜•

 

Fragment.kt

class TestFragment : Fragment() {

    // 1. ์ถ”๊ฐ€
    lateinit var binding: FragmentTestBinding

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

    }

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        // 2. ์ถ”๊ฐ€
        binding = DataBindingUtil.inflate(inflater, R.layout.fragment_test, container, false)

        // ์˜ˆ์ œ
        binding.fragmentText.text = "๋ณ€๊ฒฝ๋œ Fragment ํ…์ŠคํŠธ"

        // 3. ์ถ”๊ฐ€
        return binding.root
    }

}

 

728x90
๋ฐ˜์‘ํ˜•