[null,null,["最后更新时间 (UTC):2024-08-23。"],[],[],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)"]]