【问题标题】:Binding Adapter not working properly绑定适配器无法正常工作
【发布时间】:2017-05-22 20:20:47
【问题描述】:

我很难让@BindingAdapter 在我的项目中工作。

@BindingAdapter("imageUrl")
public static void setImageUrl(ImageView imageView, String url) {
    Log.d("TEST","URL: " + url);
}

上面的代码显示了它是如何在我的 ViewModel 中实现的。没什么特别的。

    <ImageView
        android:id="@+id/image_holder"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:scaleType="centerCrop"
        android:layout_below="@id/profile_container"
        app:imageUrl="@{item.imageUrl}"
        tools:src="@drawable/placeholder_image"/>

这不起作用。命名空间应用程序未绑定。所以我错过了什么。我试过以下 https://medium.com/google-developers/android-data-binding-custom-setters-55a25a7aea47#.6ygaiwooh 看看他们是如何设置 bindingAdapter 的。但是我错过了一些东西

【问题讨论】:

  • 您是否在布局标签中添加了命名空间以及xmlns:android?如果没有,您还应该添加xmlns:app="http://schemas.android.com/apk/res-auto"

标签: android data-binding android-databinding android-binding-adapter


【解决方案1】:

我遇到了同样的问题,我错过了使用以下方式绑定布局:

DataBindingUtil.setContentView(activity, layoutResId);

【讨论】:

  • tnx man 我用这个也为我工作 DataBindingUtil.inflate( LayoutInflater.from(context), R.layout.invite_dialog, null, false);
  • @Swapnil:请您回答类似问题:stackoverflow.com/questions/70756604/…
【解决方案2】:

我建议对可绑定属性使用 "bind" 命名空间,并为适配器参数和布局属性使用相同的名称。

适配器:

@BindingAdapter("bind:imageUrl")
public static void setImageUrl(ImageView imageView, String imageUrl) {
     Log.d("TEST","URL: " + imageUrl);
}

布局:

<ImageView
    android:id="@+id/image_holder"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:scaleType="centerCrop"
    android:layout_below="@id/profile_container"

    bind:imageUrl="@{item.imageUrl}"

    tools:src="@drawable/placeholder_image"/>

其中命名空间 "app" 被替换为 "bind"。在您的布局根目录上:

 xmlns:bind="http://schemas.android.com/apk/res-auto"

【讨论】:

    【解决方案3】:

    请记住将以下行添加到您应用的 build.gradle 文件中:

    apply plugin: 'kotlin-kapt'
    

    并检查@BindingAdapter 语法:

    @BindingAdapter("visibleOnScreen")
    fun View.setVisibility(isVisible: ObservableBoolean) {
        if (isVisible.get())
            this.visibility = View.VISIBLE
        else
            this.visibility = View.GONE
    }
    

    在视图的 xml 中:

    <TextView
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       app:visibleOnScreen="@{viewModel.errorOccurred}" />
    

    视图模型:

    var errorOccurred = androidx.databinding.ObservableBoolean(false)
    

    【讨论】:

      【解决方案4】:

      将以下行添加到您应用的 build.Gradle 文件中:

      apply plugin: 'kotlin-kapt'
      

      并在应用的 build.Gradle 文件中的 android 块中启用数据绑定:

      android {
      .
      .
      buildFeatures {
          dataBinding true
      }
      .
      .
      }
      

      创建 BindingAdapters.kt 文件并使用以下语法添加@BindingAdapter:

      @BindingAdapter("imageUrl")
        fun ImageView.bindImage(imgUrl: String){
        Glide.with(context)
        .load(imgUrl)
        .into(this)
        }
      

      在视图的 xml 中使用 ImageView 中的 imageUrl='@{"YourImageUrl"}' 语法:

       <ImageView
       android:id="@+id/imageView3"
       android:layout_width="100dp"
       android:layout_height="100dp"
       android:background="@drawable/img"
       imageUrl='@{"YourImageUrl"}'/>
      

      这很好用;)

      我的项目 Gradle 版本:7.0.0

      【讨论】:

        【解决方案5】:

        您在 xml 中使用 app:imageUrl="@{item.imageUrl}",请确保您的模型中有一个名为 imageUrl 的字符串。

        您必须传递图像的 url 链接,而不是 binderAdapter 的名称。

        语法:

        app:BinderAdapterName="@{item.imagelink}"
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2015-11-18
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多