UI 요소와 데이터를 프로그램적 방식으로 연결하지 않고, 선언적 형식으로 결합할 수 있게 도와주는 라이브러리.
프로그래밍적 방식이라 하면 기존에 코드 내에서 UI 요소를 가져와 findViewById 로 데이터와 결합해준 후 데이터를 할당하는 방식이다.
선언적 형식은 코드 내에서 UI 요소를 호출할 필요 없이 레이아웃 파일(xml)에서 직접 할당해주는 방식이다.
그러므로 XML에 있는 view에 findViewById를 하지 않고 자동으로 접근이 가능하다.
⚡이전버전
/*코틀린인 경우 plugin 추가*/
apply plugin: 'kotlin-kapt'
databinding{
enable = ture
}
⚡최신버전
/*코틀린인 경우 plugin 추가*/
plugins {
id 'kotlin-kapt'
}
buildFeatures{
dataBinding = true
}
<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"
xmlns:bind = "http://schemas.android.com/apk/res-auto">
<data>
<variable
name="activity"
type="com.example.jetpackapp.databinding.DataBindingActivity" />
</data>
<androidx.constraintlayout.widget.ConstraintLayout...>
</layout>
lateinit var binding : ActivityDatabindingBinding
binding = DataBindingUtil.setContentView(this, R.layout.activity_databinding)
binding.textTest.text = "test"
👉Next
다음 포스트에서는 xml에서 data태그를 사용하여 레이아웃 파일에서 직접 View에 텍스트를 할당하는 실습을 진행할 것 이다.