Vintage appMaker의 Tech Blog

AndroidX - DataBinding 본문

Source code or Tip/Android(Java, Kotlin)

AndroidX - DataBinding

VintageappMaker 2020. 7. 16. 10:17

AndroidX는 기존 분산된 라이브러리를 통합관리하며 편리함을 제공하고 있다. 이전의 UI 라이브러리들은 버전호환성으로 적지않은 문제가 있었기 때문이다. 

 

AndroidX에서 기본적으로 추가되는 기능인 DataBinding은 Vue.js에서 변수에 값을 넣으면 화면에 적용되는 Binding과 같은 기능을 제공한다. 

 

https://github.com/VintageAppMaker/DataBindingQuickStart

 

VintageAppMaker/DataBindingQuickStart

UltraQuick DataBinding & ViewModel경험하기 . Contribute to VintageAppMaker/DataBindingQuickStart development by creating an account on GitHub.

github.com

DataBindingQuickStart

UltraQuick DataBinding & ViewModel경험하기

MVVM의 기본이 되는 LiveData, ViewModel, DataBinding을 직관적으로 이해하기 위한 소스

app의 build.gradle에서 

plugin에서 다음추가 
apply plugin: 'kotlin-kapt'

...

dependencies에서 다음추가  

kapt "com.android.databinding:compiler:3.0.1"

dataBinding {
    enabled = true
}

DataBinding

  • XML에서는 layout으로 시작하고 그 안에 data와 view를 분리한다.

<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
>
    <data>
        <variable
                name="BindData"
                type="com.example.databindingquickstart.BindData" />

    </data>

    <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical"
    >

        <TextView
                android:id="@+id/txt_title"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:padding="16dp"
                android:text="@{BindData.showMessage}"
        />

        <TextView
                android:id="@+id/txt_title2"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:textColor="#FF0000"
                android:padding="16dp"
                android:text="@{BindData.showMessage}"
        />

        <TextView
                android:id="@+id/txt_title3"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:textColor="#00BCD4"
                android:padding="16dp"
                android:text="@{BindData.showMessage}"
        />

        <EditText
                android:id="@+id/edt_message"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"/>

    </LinearLayout>

</layout>

빌드가 안될 시

  1. File -> Invalidate Caches / Restart
  2. Build -> Clean Project
  3. Build -> Rebuild Project
  4. Run & Debug시에 빌드가 안되면 File -> Settings에서 Instant Run을 비활성화

'Source code or Tip > Android(Java, Kotlin)' 카테고리의 다른 글

JavaToKotlin  (0) 2020.07.18
Firebase Template with kotlin  (0) 2020.07.18
[Android] Glide를 이용한 animated gif, webp 이용  (0) 2020.07.18
ConstraintLayout QuickStart  (0) 2020.07.16
Github Searcher  (0) 2020.07.16
Comments