簡體   English   中英

綁定適配器無法正常工作

[英]Binding Adapter not working properly

我很難讓@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。 但是我錯過了一些東西

我遇到了同樣的問題,我錯過了使用以下方法綁定布局:

DataBindingUtil.setContentView(activity, layoutResId);

我建議對可綁定屬性使用“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"

請記住build.gradle添加到您應用的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)

將以下行添加到您應用的 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

我只是在 build.gradle(:app) 的 android 塊中的 compileOptions 和 kotlinOptions 之后放置了 buildFeatures 塊。 有效

compileOptions {
    sourceCompatibility JavaVersion.VERSION_1_8
    targetCompatibility JavaVersion.VERSION_1_8
}
kotlinOptions {
    jvmTarget = '1.8'
}
buildFeatures {
 databinding true
}

我有同樣的問題,其他解決方案都不適合我。 然后這奏效了。

活動中

dataBinding.lifecycleOwner = this

試試看

@BindingAdapter({"bind:imageUrl"})

您在 xml 中使用app:imageUrl="@{item.imageUrl}" ,請確保您的模型中有一個名為imageUrl的字符串。

您必須傳遞圖像的 url 鏈接,而不是 binderAdapter 的名稱。

句法 :

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM