在Android中,绑定适配器的作用是什么?

11

我一直在阅读关于 Android 中的绑定适配器的文章,但似乎不太理解它。 什么时候需要使用绑定适配器? 能否给出一个简单的示例来解释一下?

我读过一篇关于主活动中绑定适配器的文章。该绑定适配器有一个参数"toastMessage",显然与带有该属性的 viewModel 类中的"toastMessage"发生改变时,用注释标记的方法将被调用。

我不明白为什么我们需要这样做。

请给出一个简单的示例加以解释。

谢谢!

2个回答

14

绑定适配器用于为您的视图的某些属性设置自定义setter。我能想到的最常见的用例是将图像设置为ImageView,其中图像的加载大多在UI线程之外完成。

我们大多数人都有我们喜欢的图像加载库来加载图像。对于每个要加载的图像,您都需要编写代码从远程(或本地)加载URL并将其设置为我们的图像视图。当您在每个图像视图上看到此样板代码时,您可以使用一些实用程序方法。

绑定适配器使这更加简单。您可以在XML中设置属性,数据绑定库会查找绑定适配器以将该属性设置为您的视图。由于数据是可观察的,因此每当数据更改时,更改将触发到视图。

<ImageView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    app:imageUrl="@{data.imageUrl}"/>
@BindingAdapter("imageUrl")
public static void setImageUrl(ImageView imageView, String url) {
    if (url == null) {
        imageView.setImageDrawable(null);
    } else {
        Picasso.get().load(url).into(imageView); // replace with your fav image loading lib
    }
}

文档提供了几个例子,您可以在这些情况下使用它。George Mount的文章也非常清楚地解释了如果您正在使用数据绑定,何时以及为什么要使用此功能。


2

绑定适配器负责调用相应的框架方法来设置值。

当您将数据绑定引入您的应用程序时,将值设置到您的视图中是很常见的事情。就像下面的例子:

 <ProgressBar
         app:layout_constraintLeft_toLeftOf="parent"
         app:layout_constraintRight_toRightOf="parent"
         app:layout_constraintTop_toTopOf="parent"
         app:layout_constraintBottom_toBottomOf="parent"
         android:visibility="@{viewmodel.loadingData?View.VISIBLE:View.GONE}"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"/>

有时,您希望在设置方法中执行一些特殊操作。例如,您在XML中有一个RecyclerView并且想定义它的适配器,您可以通过为其在代码中定义一个bindingAdapter来实现。 "bindingAdapter"可以理解为绑定适配器的意思。
<android.support.v7.widget.RecyclerView
            android:id="@+id/rv_main_articles"
            app:adapter="@{viewmodel.articles}"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintLeft_toLeftOf="parent"
            android:layout_width="0dp"
            android:layout_height="0dp"/>

绑定适配器主要有两种用法:

1- 用于RecyclerView:

@JvmStatic
        @BindingAdapter("app:adapter")
        fun <T> setItems(recyclerView: RecyclerView, items: List<T>?) {

            Log.d(TAG, "articles:[$items]")

            if (recyclerView.adapter is HomeAdapter) {

                if (items != null) {
                    (recyclerView.adapter as HomeAdapter).swapData(items)
                }

            }
        }

2- 要将图像加载到您的ImageView


@JvmStatic
        @BindingAdapter("app:loadImage")
        fun setImageResource(view: ImageView, imageUrl: String?) {

            val context = view.context

            val options = RequestOptions()
                .diskCacheStrategy(DiskCacheStrategy.RESOURCE)


            imageUrl?.let {

                Glide.with(context)
                    .setDefaultRequestOptions(options)
                    .load(imageUrl)
                    .transition(DrawableTransitionOptions.withCrossFade(1000))
                    .into(view)
            }
        }

更多信息: 链接

最初的回答


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接