Notice
Recent Posts
Recent Comments
Link
250x250
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
Tags
- Button
- 앱
- 앱바
- binding
- 안드로이드
- tabbar
- Coroutines
- Navigation
- TEST
- LifeCycle
- ScrollView
- viewmodel
- textview
- livedata
- appbar
- intent
- DART
- drift
- scroll
- Dialog
- Compose
- Kotlin
- textfield
- CustomScrollView
- data
- android
- 계측
- 테스트
- Flutter
- activity
Archives
- Today
- Total
Study Record
[안드로이드] 데이터 바인딩(Data Binding) 참고사항 본문
728x90
😶 text 로 데이터 접근하기
@={} 기호 사이에 변수를 명시한다.
<?xml version="1.0" encoding="utf-8"?>
<layout>
<data>
<variable
name="myInfo"
type="com.example.basic_text.MyName" />
</data>
...
<TextView
android:id="@+id/name_tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@={myInfo.name}"
android:textColor="@color/black"
android:textSize="18sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
...
</layout>
😶 strings.xml 파일에서 문자열에 접근하여 사용할 경우
<string name="test_string">Total %d</string>
문자열 데이터에 %d 와 같이 값을 넣어줘야 하는 경우 @string/test_string 과 같이 데이터를 불러오는 형식에 @{} 로 감싸고 값을 넣어주는 부분은 @string/test_string 뒤에 괄호로 데이터 값을 명시한다.
<?xml version="1.0" encoding="utf-8"?>
<layout>
<data>
<variable
name="myInfo"
type="com.example.basic_text.MyName" />
</data>
...
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@{@string/test_string(myInfo.quantity)}" />
...
</layout>
😶 자동 true/false 값 업데이트
예를 들어, 사용자가 선택해야할 옵션들이 있다면 android:checked 속성을 사용할 수 있다. viewModel 의 flavor 변수의 값에 따라 equals() 함수로 같은 옵션이 있으면 체크하고 아니면 체크하지 않는다.
<?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"
tools:context=".FlavorFragment">
<data>
<variable
name="viewModel"
type="com.example.cupcake.model.OrderViewModel" />
</data>
// ...
<RadioGroup
android:id="@+id/flavor_options"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<RadioButton
android:id="@+id/vanilla"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="@{viewModel.flavor.equals(@string/vanilla)}"
android:text="@string/vanilla" />
<RadioButton
android:id="@+id/chocolate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="@{viewModel.flavor.equals(@string/chocolate)}"
android:text="@string/chocolate" />
<RadioButton
android:id="@+id/red_velvet"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="@{viewModel.flavor.equals(@string/red_velvet)}"
android:text="@string/red_velvet" />
</RadioGroup>
// ...
</layout>
😶 함수 실행하기
클릭했을 때 실행할 함수를 바로 입력할 수 있다. 다음 예시는 next_button 을 눌렀을 때 viewModel.checked() 함수가 실행된다.
<?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"
tools:context=".FlavorFragment">
<data>
<variable
name="viewModel"
type="com.example.cupcake.model.OrderViewModel" />
</data>
// ...
<Button
android:id="@+id/next_button"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="@string/next"
android:onClick="@{() -> viewModel.checked()}"/>
// ...
</layout>
😶 UI 업그레이드
binding.invalidateAll()
binding.executePendingBindings()
invalidateAll() 메서드는 모든 binding 식을 무효화하고 UI를 새로 고치도록 새 binding을 요청한다.
executePendingBindings() 메서드는 수정된 변수에 바인딩된 식을 가진 보기를 업데이트하여 보류 중인 바인딩을 평가합니다. 이는 UI 스레드에서 실행되어야 한다.
728x90
'안드로이드' 카테고리의 다른 글
[안드로이드] Task 와 Back Stack (0) | 2023.08.02 |
---|---|
[안드로이드] LiveData 참고사항 (0) | 2023.08.02 |
[안드로이드] ViewModel 참고사항 (0) | 2023.08.02 |
[안드로이드] navigation 참고사항 (0) | 2023.08.01 |
[안드로이드] ViewModel 과 LiveData & Data Binding (0) | 2023.07.28 |