안드로이드-데이터 바인딩

김민준·2023년 3월 31일

먼저 data binding이란 데이터 결합을 의미합니다.
데이터 바인딩은 레이아웃의 뷰를 결합하여 간단하게 xml파일에 data를 연결해서 사용하는 방법입니다.
Activity에서 따로 View를 정의 해서 사용할 것 없이 Dataview를 연결시키면 data 변화할때 따로 세팅해주지 않아도 변경되게 할 수 있습니다.

android{
	
    ...
    dataBinding{
   		enabled = true
    }
}

위의 코드를 build.gradle 파일에 추가 시켜줍니다.

DataBinding을 위한 xml파일수정

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <TextView
        android:id="@+id/text_view"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World!"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"/>

</androidx.constraintlayout.widget.ConstraintLayout>

처음 생성할때 만들어지는 android_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/text_view"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Hello World!"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintBottom_toBottomOf="parent"/>

    </androidx.constraintlayout.widget.ConstraintLayout>
</layout>

위의 처럼 ConstraintLayout바깥에 layout을 감싸서 파일을 바꿔 주면 됩니다.

레이아웃 파일 data요소 추가

<?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>
    	...
    </data>
    -------------------------------------------------
    <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".MainActivity">

        <TextView
            android:id="@+id/text_view"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Hello World!"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintBottom_toBottomOf="parent"/>

    </androidx.constraintlayout.widget.ConstraintLayout>
</layout>

후에 바인딩을 위해 Activity에 binding setting을 해주면 끝입니다.

binding.activity = this
binding.btnChange.setOnClickListener {
    text = "test"
    binding.invalidateAll()
}
  • bindinb.activity : xml에서 정의한 data의 name

  • binding.invalidateAll() : data가 변한 후 연결된 view들에 변화르 알려주는 함수

후에는 activity에서 위에 처럼 정의하면 됩니다.

0개의 댓글