کتابخانه Data Binding یک کتابخانه پشتیبانی است که به شما امکان می دهد اجزای رابط کاربری را در طرح بندی های خود به منابع داده در برنامه خود با استفاده از فرمت اعلامی و نه برنامه نویسی متصل کنید.
طرحبندیها اغلب در فعالیتهایی با کدی تعریف میشوند که متدهای چارچوب UI را فراخوانی میکند. برای مثال، کد زیر findViewById() را فراخوانی می کند تا یک ویجت TextView را پیدا کند و آن را به ویژگی userName متغیر viewModel متصل کند:
کاتلین
findViewById<TextView>(R.id.sample_text).apply {
text = viewModel.userName
}
مثال زیر نحوه استفاده از Data Binding Library برای اختصاص متن به ویجت به طور مستقیم در فایل طرح بندی را نشان می دهد. این کار نیاز به فراخوانی کدهای جاوا را که در بالا نشان داده شده است را از بین می برد. به استفاده از دستور @{} در عبارت انتساب توجه کنید:
<TextView
android:text="@{viewmodel.userName}" />
اتصال مؤلفهها در فایل طرحبندی به شما امکان میدهد بسیاری از فراخوانهای چارچوب UI را در فعالیتهای خود حذف کنید، و نگهداری آنها را سادهتر و آسانتر میکند. این همچنین می تواند عملکرد برنامه شما را بهبود بخشد و به جلوگیری از نشت حافظه و استثناهای اشاره گر تهی کمک کند.
با استفاده از کتابخانه Data Binding
از صفحات زیر برای یادگیری نحوه استفاده از Data Binding Library در برنامه های Android خود استفاده کنید.
زبان عبارت به شما امکان می دهد عباراتی بنویسید که متغیرها را به نماهای موجود در طرح بندی متصل می کند. کتابخانه Data Binding به طور خودکار کلاس های مورد نیاز برای اتصال نماهای موجود در طرح با اشیاء داده شما را ایجاد می کند. این کتابخانه ویژگی هایی مانند واردات، متغیرها و شامل مواردی را ارائه می دهد که می توانید در طرح بندی های خود از آنها استفاده کنید.
این ویژگی های کتابخانه به طور یکپارچه با طرح بندی های موجود شما همزیستی دارند. برای مثال، متغیرهای اتصالی که میتوانند در عبارات استفاده شوند، در داخل یک عنصر data تعریف میشوند که برادر عنصر ریشه طرحبندی UI است. همانطور که در مثال زیر نشان داده شده است، هر دو عنصر در یک تگ layout پیچیده شده اند:
کتابخانه Data Binding کلاس ها و روش هایی را برای مشاهده آسان داده ها برای تغییرات فراهم می کند. وقتی منبع داده اصلی تغییر می کند، لازم نیست نگران به روز کردن رابط کاربری باشید. شما می توانید متغیرهای خود یا ویژگی های آنها را قابل مشاهده کنید. کتابخانه به شما امکان می دهد اشیا، فیلدها یا مجموعه ها را قابل مشاهده کنید.
کتابخانه Data Binding کلاس های اتصالی را تولید می کند که برای دسترسی به متغیرها و نماهای طرح بندی استفاده می شود. این صفحه نحوه استفاده و سفارشی سازی کلاس های binding تولید شده را به شما نشان می دهد.
برای هر عبارت طرح بندی، یک آداپتور الزام آور وجود دارد که فریم ورک مورد نیاز را برای تنظیم ویژگی ها یا شنوندگان مربوطه فراخوانی می کند. به عنوان مثال، آداپتور binding می تواند از فراخوانی setText() برای تنظیم ویژگی متن مراقبت کند یا متد setOnClickListener() را فراخوانی کند تا شنونده ای به رویداد کلیک اضافه کند. متداولترین آداپتورهای اتصال، مانند آداپتورهای ویژگی android:text که در مثالهای این صفحه استفاده میشوند، برای استفاده در بسته android.databinding.adapters در دسترس شما هستند. برای فهرستی از آداپتورهای اتصال رایج، به آداپتورها مراجعه کنید. شما همچنین می توانید آداپتورهای سفارشی ایجاد کنید، همانطور که در مثال زیر نشان داده شده است:
کاتلین
@BindingAdapter("app:goneUnless")
fun goneUnless(view: View, visible: Boolean) {
view.visibility = if (visible) View.VISIBLE else View.GONE
}
کتابخانه پشتیبانی اندروید شامل اجزای معماری است که می توانید از آنها برای طراحی برنامه های قوی، قابل آزمایش و قابل نگهداری استفاده کنید. می توانید از اجزای معماری با کتابخانه Data Binding برای ساده سازی بیشتر توسعه UI خود استفاده کنید.
کتابخانه Data Binding از اتصال دو طرفه داده پشتیبانی می کند. نماد مورد استفاده برای این نوع اتصال از توانایی دریافت تغییرات داده در یک ویژگی و گوش دادن به به روز رسانی های کاربر برای آن ویژگی به طور همزمان پشتیبانی می کند.
منابع اضافی
برای کسب اطلاعات بیشتر در مورد اتصال داده ها، به منابع اضافی زیر مراجعه کنید.
محتوا و نمونه کدها در این صفحه مشمول پروانههای توصیفشده در پروانه محتوا هستند. جاوا و OpenJDK علامتهای تجاری یا علامتهای تجاری ثبتشده Oracle و/یا وابستههای آن هستند.
تاریخ آخرین بهروزرسانی 2024-11-13 بهوقت ساعت هماهنگ جهانی.
[[["درک آسان","easyToUnderstand","thumb-up"],["مشکلم را برطرف کرد","solvedMyProblem","thumb-up"],["غیره","otherUp","thumb-up"]],[["اطلاعاتی که نیاز دارم وجود ندارد","missingTheInformationINeed","thumb-down"],["بیشازحد پیچیده/ مراحل بسیار زیاد","tooComplicatedTooManySteps","thumb-down"],["قدیمی","outOfDate","thumb-down"],["مشکل ترجمه","translationIssue","thumb-down"],["مشکل کد / نمونهها","samplesCodeIssue","thumb-down"],["غیره","otherDown","thumb-down"]],["تاریخ آخرین بهروزرسانی 2024-11-13 بهوقت ساعت هماهنگ جهانی."],[],[],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)"]]