Veri Bağlama Kitaplığı, kullanıcı arayüzünü bağlamanıza olanak tanıyan bir destek kitaplığıdır
düzeninizdeki bileşenleri, bildirim temelli kullanarak uygulamanızdaki veri kaynaklarına
biçimiyle zaman kaybetmeyin.
Düzenler genellikle kullanıcı arayüzü çerçevesini çağıran koda sahip etkinliklerde tanımlanır
yöntemlerine göz atın. Örneğin, aşağıdaki kod bir TextView widget'ı bulmak ve bunu şunun userName özelliğine bağlamak için findViewById() yöntemini çağırır:
viewModel değişkeni:
Kotlin
findViewById<TextView>(R.id.sample_text).apply {
text = viewModel.userName
}
Aşağıdaki örnekte metin atamak için Veri Bağlama Kitaplığı'nın nasıl kullanılacağı gösterilmektedir
widget'a gönderebilirsiniz. Bu sayede, projedeki herhangi bir
yukarıda gösterilen Java kodunu yazın. Ödevde @{} söz dizimi kullanımına dikkat edin
ifade:
<TextView
android:text="@{viewmodel.userName}" />
Düzen dosyasındaki bileşenleri bağlamak, birçok kullanıcı arayüzü çerçevesi çağrısını
Böylece onları daha basit ve kolay bir şekilde sürdürebilirsiniz. Bu işlem ayrıca
Uygulamanızın performansını iyileştirir, bellek sızıntılarını ve boş işaretçiyi önlemeye yardımcı olur
istisnalar olabilir.
Geliştirme ortamınızı Veriler'le çalışmaya hazır hale getirmeyi öğrenin
Android Studio'da veri bağlama kodu desteği de dahil olmak üzere bağlama kitaplığı.
İfade dili, ifadelerle kendinizi ifade eden
değişkenlerini düzendeki görünümlere ekleyin. Veri Bağlama Kitaplığı otomatik olarak
oluştur, düzendeki görünümleri verilerinizle bağlamak için gereken sınıfları oluşturur
nesneler'i tıklayın. Kitaplıkta içe aktarma işlemleri, değişkenler ve
özellikleri de içerir.
Kitaplığın bu özellikleri, mevcut düzenlerinizle sorunsuz bir şekilde birlikte kullanılır.
Örneğin, ifadelerde kullanılabilecek bağlama değişkenleri
(kullanıcı arayüzü düzeninin kök öğesinin eşdüzey bir öğe olan) data öğesinin içinde olmalıdır.
Her iki öğe de aşağıda gösterildiği gibi bir layout etiketi içine sarmalanır.
örnek:
Veri Bağlama Kitaplığı, verileri kolayca gözlemlemek için sınıflar ve yöntemler sunar
kontrol edin. Bu işlem tamamlandığında kullanıcı arayüzünü yenileme
temel veri kaynağı değişikliklerinden
bahsedeceğiz. Değişkenlerinizi veya değişkenlerinizi
gözlemlenebilir özellikler. Kitaplıkta nesne, alan veya alan oluşturmanıza
gözlemlenebilir koleksiyonlar.
Veri Bağlama Kitaplığı,
ve görünümlerini oluşturabilirsiniz. Bu sayfada, DMAIC ve Yalın Altı Sigma yaklaşımının nasıl kullanılacağı
bağlama sınıfları oluşturdu.
Her düzen ifadesi için şunları sağlayan bir bağlama bağdaştırıcısı vardır:
ilgili özellikleri veya işleyicileri ayarlamak için gereken çerçeve çağrılarını gösterir. Örneğin,
örnek olarak, bağlama bağdaştırıcısı, setText() yöntemini çağırma işlemini yapabilir
veya setOnClickListener() yöntemini çağırarak
işleyici olabilir. Aşağıdaki gibi en yaygın bağlama bağdaştırıcıları,
bu sayfadaki örneklerde kullanılan android:text özelliği için bağdaştırıcılar
android.databinding.adapters paketinde kullanabilirsiniz.
Ortak bağlama bağdaştırıcılarının listesi için bkz.
bağdaştırıcıları kullanın.
Aşağıdaki örnekte gösterildiği gibi özel bağdaştırıcılar da oluşturabilirsiniz:
'nı inceleyin.
Kotlin
@BindingAdapter("app:goneUnless")
fun goneUnless(view: View, visible: Boolean) {
view.visibility = if (visible) View.VISIBLE else View.GONE
}
Android Destek Kitaplığı, Mimari'yi içerir
Bileşenler'i tıklayın.
Sağlam, test edilebilir ve bakımı yapılabilir uygulamalar tasarlamalıdır. Mimariyi kullanarak
Geliştirmeyi daha da basitleştirmek için Veri Bağlama Kitaplığı'ndaki bileşenler
önemli bir nokta.
Veri Bağlama Kitaplığı, iki yönlü veri bağlamayı destekler. Şunun için kullanılan gösterim:
bu tür bağlama, bir mülkte yapılan veri değişikliklerini alabilme özelliğini destekler
mülkteki kullanıcı güncellemelerini aynı anda dinleyebilirsiniz.
Ek kaynaklar
Veri bağlama hakkında daha fazla bilgi için aşağıdakilere bakın
ek kaynaklar.
Bu sayfadaki içerik ve kod örnekleri, İçerik Lisansı sayfasında açıklanan lisanslara tabidir. Java ve OpenJDK, Oracle ve/veya satış ortaklarının tescilli ticari markasıdır.
Son güncelleme tarihi: 2024-08-23 UTC.
[[["Anlaması kolay","easyToUnderstand","thumb-up"],["Sorunumu çözdü","solvedMyProblem","thumb-up"],["Diğer","otherUp","thumb-up"]],[["İhtiyacım olan bilgiler yok","missingTheInformationINeed","thumb-down"],["Çok karmaşık / çok fazla adım var","tooComplicatedTooManySteps","thumb-down"],["Güncel değil","outOfDate","thumb-down"],["Çeviri sorunu","translationIssue","thumb-down"],["Örnek veya kod sorunu","samplesCodeIssue","thumb-down"],["Diğer","otherDown","thumb-down"]],["Son güncelleme tarihi: 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)"]]