android databinding architecture를 테스트하다 겪은 여러가지 실수에 대해서 정리해 본다.


data Binding을 사용하기 위해서는 build.gradle 에 다음과 같이 추가해야 하는걸 잊지 말자

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

1. Binding Class 이름 규칙을 잘 기억하자

BindingClass의 이름은 activity xml파일명을 Camel case로 변경한 것이다.

activity_main.xml인 경우 AndroidMainBinding이다, MainActivityBinding이 아니다

ActivityMainBinding binding = DataBindingUtil.setContentView(this, R.layout.activity_main);


2. root element는 <layout>이다

이제 기존 ui layout(eg. ConstraintLayout)은 root element인 'layout' 내에 포함되어야 한다.

<?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"
>
...
<android.support.constraint.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">


3. <data>선언을 nested layout에 포함시키지 말자.

data은 root element인 'layout' 의 child에 존재한다.

<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="hello" type="works.papa.labs.databindingtest.binding.Hello"/>
</data>

<android.support.constraint.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">

Happy Coding~!

+ Recent posts