일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | ||||
4 | 5 | 6 | 7 | 8 | 9 | 10 |
11 | 12 | 13 | 14 | 15 | 16 | 17 |
18 | 19 | 20 | 21 | 22 | 23 | 24 |
25 | 26 | 27 | 28 | 29 | 30 | 31 |
- mongoose
- lifecycle
- vuex
- union
- class component
- Kotlin
- function
- Service
- docker
- 생명주기
- elementAt
- Swift
- MINUS
- list
- react native
- Foreign Key
- LiveData
- map
- Interface
- ReactNative
- collection
- ConstraintLayout
- docker-compose
- Filter
- recyclerview
- AWS
- Generic
- enum
- animation
- CLASS
- Today
- Total
개발 일기
Databinding ? 본문
데이터바인딩이랑 Android Jetpack의 구성요소로서 선언적 형식으로 레이아웃의 UI 구성 요소를 앱의 데이터 소스와 결합할수 있는 지원 라이브러리입니다.
기존에서는 앱의 UI를 변화하기 위해서는 아래와 같은 코드가 필요 했지만
findViewById<TextView>(R.id.sample_text).apply {
text = viewModel.userName
}
이제는 데이터바인딩을 사용하여 직접 위젯에 텍스트를 할당하는 방법이 있습니다.
<TextView
android:text="@{viewmodel.userName}" />
빌드 환경 :)
databinding을 사용하기 위해서는 아래와 같이 선언이 필요합니다.
android {
...
dataBinding {
enabled = true
}
}
사용 방법 :)
아래와 같이 UI Layout 루트 요소의 동위 요소인 data 요소에 정의됩니다. 두 요소는 다음과 같이 layout에 정의됩니다.
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<data>
<variable
name="viewmodel"
type="com.myapp.data.ViewModel" />
</data>
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@{viewmodel.firstName}" />
</layout>
data 내의 viewmodel 변수는 이 레이아웃 내에서 사용할 수 있는 속성을 설명합니다.
레이아웃에서 표현식은 '@{}' 구문을 사용하여 속성(attribute properties)에서 작성됩니다. 여기서 TextView 텍스트는 viewmodel 변수의 firstName 속성으로 설정됩니다.
일반적인 기능
표현식 언어는 관리형 코드에서 볼 수 있는 표현식과 매우 비슷합니다. 표현식 언어에서는 다음 연산자와 키워드를 사용할 수 있습니다.
- 산술 연산자 + - / * %
- 문자열 연결 연산자 +
- 논리 연산자 && ||
- 바이너리 연산자 & | ^
- 단항 연산자 + - ! ~
- 이동 연산자 >> >>> <<
- 비교 연산자 == > < >= <=(<는 <으로 이스케이프 처리되어야 함)
- instanceof
- 그룹화 ()
- 리터럴 - 문자, 문자열, 숫자, null
- 변환
- 메서드 호출
- 필드 액세스
- 배열 액세스 []
- 삼항 연산자 ?:
아래는 예시입니다. :)
android:text="@{String.valueOf(index + 1)}"
android:visibility="@{age > 13 ? View.GONE : View.VISIBLE}"
android:transitionName='@{"image_" + id}'
Null 병합 연산자 :)
null 병합 연산자는 왼쪽 피 연산자가 null이 아니면 왼쪽 피 연산자를 선택하고 null이면 오른쪽을 선택합니다.
android:text="@{user.displayName ?? user.lastName}"
컬렉션 :)
배열, 목록, 희소 목록 및 맵과 같은 일반 컬렉션에는 편의상 [] 연산자를 사용하여 액세스할 수 있습니다.
<data>
<import type="android.util.SparseArray"/>
<import type="java.util.Map"/>
<import type="java.util.List"/>
<variable name="list" type="List<String>"/>
<variable name="sparse" type="SparseArray<String>"/>
<variable name="map" type="Map<String, String>"/>
<variable name="index" type="int"/>
<variable name="key" type="String"/>
</data>
…
android:text="@{list[index]}"
…
android:text="@{sparse[index]}"
…
android:text="@{map[key]}"
리소스 :)
다음 구문을 사용하여 표현식의 리소스에 액세스할 수 있습니다
android:padding="@{large? @dimen/largePadding : @dimen/smallPadding}"
형식 문자열 및 복수형은 다음과 같이 매개변수를 지정하여 값을 구할 수 있습니다.
android:text="@{@string/nameFormat(firstName, lastName)}"
android:text="@{@plurals/banana(bananaCount)}"
리스너 관련 이벤트 등록 :)
아래와 같이 onClick 이벤트에 다양한 방식으로 이벤트를 등록할수 있습니다.
class MyHandlers {
fun onClickFriend(view: View) { ... }
}
class Presenter {
fun onSaveClick(task: Task){}
}
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android">
<data>
<variable name="handlers" type="com.example.MyHandlers"/>
<variable name="user" type="com.example.User"/>
</data>
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@{user.firstName}"
android:onClick="@{handlers::onClickFriend}"/>
</LinearLayout>
</layout>
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android">
<data>
<variable name="task" type="com.android.example.Task" />
<variable name="presenter" type="com.android.example.Presenter" />
</data>
<LinearLayout android:layout_width="match_parent" android:layout_height="match_parent">
<Button android:layout_width="wrap_content" android:layout_height="wrap_content"
android:onClick="@{(view) -> presenter.onSaveClick(task)}" />
</LinearLayout>
</layout>
'Client > 안드로이드' 카테고리의 다른 글
Service (0) | 2020.03.26 |
---|---|
paging library ? (페이징 라이브러리) (0) | 2020.03.24 |
Android Manifest ? (0) | 2020.03.21 |
WorkManager ? (0) | 2020.03.18 |
LiveData ? (0) | 2020.03.18 |