【问题标题】:What is the use of binding adapter in Android?Android中绑定适配器有什么用?
【发布时间】:2019-05-25 14:06:06
【问题描述】:

一直在看Android中绑定适配器的文章,但是好像没看懂。 什么时候使用绑定适配器? 有人可以用一个简单的例子来解释吗?

我阅读的一篇文章在 Main Activity 中有一个绑定适配器。绑定适配器有一个参数“toastMessage”,显然,只要“toastMessage”(viewModel 类中的一个属性)发生更改,就会调用使用此绑定适配器注释的方法。

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

用一个简单的例子来解释会很有帮助。

谢谢!

【问题讨论】:

    标签: android android-databinding


    【解决方案1】:

    绑定适配器用于对视图的某些属性进行自定义设置。我能想到的最常见的用例是将图像设置为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
        }
    }
    

    doc 提供了几个这样的示例,您可以在其中使用它。 George Mount 的这个article 也非常清楚地解释了如果您使用数据绑定,您可能希望在哪里以及为什么要使用它。

    【讨论】:

      【解决方案2】:

      绑定适配器负责进行适当的框架调用以设置值。

      当您在应用程序中使用data binding 时,将值设置到您的views 中是很正常的事情。如下例:

       <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 来实现它。

      <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(s)

      @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)
                  }
              }
      

      更多信息:link

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2019-07-19
        • 1970-01-01
        • 2020-09-27
        • 2017-12-15
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多