728x90

2019/09/02 - [Dev/Android] - [Android] 데이터 바인딩 개념 및 라이브러리 비교(Android Data Binding Library, Kotlin Android Extensions)

 

 

안드로이드 앱개발 시

데이터바인딩 설정하는건 정말 간단한데, 그것마저도 가끔씩 까먹어서 요약글 적어놓습니다.

 

 

1. build.gradle (app)

1
2
3
4
5
6
7
8
android{
 
  dataBinding {
      enabled = true
  }
 
}
 
cs

 

2. XML layout

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android">
 
  <data>
        <variable
            name="activity"
            type="패키지경로.MainActivity"/>
 
    </data>
 
 
<LinearLayout
   android:layout_width="match_parent"
   android:layout_height="match_parent"
   android:orientation="vertical"/>
 
</layout>
 
cs

 

3. Java

1
2
3
4
5
6
7
8
9
10
MainActivityLayoutBinding databinding;
 
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
 
   databinding = DataBindingUtil.setContentView(this, R.layout.main_activity_layout);
   databinding.setActivity(this);
 
}
cs

 

 

 

 

 

Reference

 

Android Jetpack:Empower your UI with Android Data Binding

Data Binding Library is a support library that enables you to bind UI elements in your layouts to data sources in your app using a…

medium.com

 

728x90

+ Recent posts