데이터 결합 라이브러리는 데이터를 쉽게 관찰할 수 있는 클래스와 메서드를 제공합니다.
확인할 수 있습니다 UI를 새로고침하는 것에 대해 걱정할 필요가 없습니다.
변경할 수 있습니다 변수나 해당 변수가
속성을 살펴보겠습니다. 라이브러리를 사용하면 객체, 필드 또는
50%에 불과했습니다.
모든 레이아웃 표현식에는
해당 속성 또는 리스너를 설정하는 데 필요한 프레임워크 호출 대상
예를 들어 결합 어댑터는 setText() 메서드 호출을 처리할 수 있습니다.
를 사용하여 텍스트 속성을 설정하거나 setOnClickListener() 메서드를 호출하여
리스너입니다. 가장 일반적인 결합 어댑터는
이 페이지의 예시에 사용된 android:text 속성용 어댑터
android.databinding.adapters 패키지에서 사용할 수 있습니다.
일반적인 결합 어댑터 목록은 다음을 참고하세요.
어댑터가 있습니다.
다음 예와 같이 맞춤 어댑터를 만들 수도 있습니다.
를 통해 개인정보처리방침을 정의할 수 있습니다.
Kotlin
@BindingAdapter("app:goneUnless")
fun goneUnless(view: View, visible: Boolean) {
view.visibility = if (visible) View.VISIBLE else View.GONE
}
이 페이지에 나와 있는 콘텐츠와 코드 샘플에는 콘텐츠 라이선스에서 설명하는 라이선스가 적용됩니다. 자바 및 OpenJDK는 Oracle 및 Oracle 계열사의 상표 또는 등록 상표입니다.
최종 업데이트: 2024-08-23(UTC)
[null,null,["최종 업데이트: 2024-08-23(UTC)"],[],[],null,["# Data Binding Library\nPart of [Android Jetpack](/jetpack).\n=========================================================\n\nThe Data Binding Library is a support library that allows you to bind UI\ncomponents in your layouts to data sources in your app using a declarative\nformat rather than programmatically.\n\nLayouts are often defined in activities with code that calls UI framework\nmethods. For example, the code below calls [findViewById()](/reference/android/app/Activity#findViewById(int)) to find a [TextView](/reference/android/widget/TextView) widget and bind it to the `userName` property of the\n`viewModel` variable: \n\n### Kotlin\n\n```kotlin\nfindViewById\u003cTextView\u003e(R.id.sample_text).apply {\n text = viewModel.userName\n}\n```\n\n### Java\n\n```java\nTextView textView = findViewById(R.id.sample_text);\ntextView.setText(viewModel.getUserName());\n```\n\nThe following example shows how to use the Data Binding Library to assign text\nto the widget directly in the layout file. This removes the need to call any of\nthe Java code shown above. Note the use of `@{}` syntax in the assignment\nexpression: \n\n \u003cTextView\n android:text=\"@{viewmodel.userName}\" /\u003e\n\nBinding components in the layout file lets you remove many UI framework calls in\nyour activities, making them simpler and easier to maintain. This can also\nimprove your app's performance and help prevent memory leaks and null pointer\nexceptions.\n| **Note:** In many cases, [view binding](/topic/libraries/view-binding) can provide the same benefits as data binding with simpler implementation and better performance. If you are using data binding primarily to replace `findViewById()` calls, consider using view binding instead.\n\nUsing the Data Binding Library\n------------------------------\n\nUse the following pages to learn how to use the Data Binding Library in your\nAndroid apps.\n\n[**Get started**](/topic/libraries/data-binding/start)\n: Learn how to get your development environment ready to work with the Data\n Binding Library, including support for data binding code in Android Studio.\n\n[**Layouts and binding expressions**](/topic/libraries/data-binding/expressions)\nThe expression language allows you to write expressions that connect\nvariables to the views in the layout. The Data Binding Library automatically\ngenerates the classes required to bind the views in the layout with your data\nobjects. The library provides features such as imports, variables, and\nincludes that you can use in your layouts.\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\nThese features of the library coexist seamlessly with your existing layouts.\nFor example, the binding variables that can be used in expressions are defined\ninside a `data` element that is a sibling of the UI layout's root element.\nBoth elements are wrapped in a `layout` tag, as shown in the following\nexample:\n\n\u003cbr /\u003e\n\n \u003clayout xmlns:android=\"http://schemas.android.com/apk/res/android\"\n xmlns:app=\"http://schemas.android.com/apk/res-auto\"\u003e\n \u003cdata\u003e\n \u003cvariable\n name=\"viewmodel\"\n type=\"com.myapp.data.ViewModel\" /\u003e\n \u003c/data\u003e\n \u003cConstraintLayout... /\u003e \u003c!-- UI layout's root element --\u003e\n \u003c/layout\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n[**Work with observable data objects**](/topic/libraries/data-binding/observability)\n: The Data Binding Library provides classes and methods to easily observe data\n for changes. You don't have to worry about refreshing the UI when the\n underlying data source changes. You can make your variables or their\n properties observable. The library allows you to make objects, fields, or\n collections observable.\n\n[**Generated binding classes**](/topic/libraries/data-binding/generated-binding)\n: The Data Binding Library generates binding classes that are used to access the\n layout's variables and views. This page shows you how to use and customize\n generated binding classes.\n\n[**Binding adapters**](/topic/libraries/data-binding/binding-adapters)\n: For every layout expression, there is a binding adapter that makes the\n framework calls required to set the corresponding properties or listeners. For\n example, the binding adapter can take care of calling the `setText()` method\n to set the text property or call the `setOnClickListener()` method to add a\n listener to the click event. The most common binding adapters, such as the\n adapters for the `android:text` property used in the examples in this page,\n are available for you to use in the `android.databinding.adapters` package.\n For a list of the common binding adapters, see\n [adapters](https://android.googlesource.com/platform/frameworks/data-binding/+/refs/heads/studio-master-dev/extensions/baseAdapters/src/main/java/androidx/databinding/adapters).\nYou can also create custom adapters, as shown in the following example: \n\n### Kotlin\n\n```kotlin\n@BindingAdapter(\"app:goneUnless\")\nfun goneUnless(view: View, visible: Boolean) {\n view.visibility = if (visible) View.VISIBLE else View.GONE\n}\n```\n\n### Java\n\n```java\n@BindingAdapter(\"app:goneUnless\")\npublic static void goneUnless(View view, Boolean visible) {\n view.visibility = visible ? View.VISIBLE : View.GONE;\n}\n```\n\n[**Bind layout views to Architecture Components**](/topic/libraries/data-binding/architecture)\n: The Android Support Library includes the [Architecture\n Components](/topic/libraries/architecture), which you can use to\n design robust, testable, and maintainable apps. You can use the Architecture\n Components with the Data Binding Library to further simplify the development\n of your UI.\n\n[**Two-way data binding**](/topic/libraries/data-binding/two-way)\n: The Data Binding Library supports two-way data binding. The notation used for\n this type of binding supports the ability to receive data changes to a property\n and listen to user updates to that property at the same time.\n\nAdditional resources\n--------------------\n\nTo learn more about data binding, consult the following\nadditional resources.\n\n### Samples\n\n- [Android Data Binding Library samples](https://github.com/android/databinding-samples)\n\n### Codelabs\n\n- [Android Data Binding codelab](https://codelabs.developers.google.com/codelabs/android-databinding)\n\n### Blog posts\n\n- [Android Data Binding Library --- From Observable Fields to LiveData in two steps](https://medium.com/androiddevelopers/android-data-binding-library-from-observable-fields-to-livedata-in-two-steps-690a384218f2)\n- [Data Binding --- Lessons Learnt](https://medium.com/androiddevelopers/data-binding-lessons-learnt-4fd16576b719)\n\nRecommended for you\n-------------------\n\n- Note: link text is displayed when JavaScript is off\n- [Binding adapters {:#binding-adapters}](/topic/libraries/data-binding/binding-adapters)\n- [Layouts and binding expressions](/topic/libraries/data-binding/expressions)\n- [Generated binding classes {: #binding-classes}](/topic/libraries/data-binding/generated-binding)"]]