[英]Binding Adapter not working properly
I have a hard time making @BindingAdapter
to work in my project.我很难让
@BindingAdapter
在我的项目中工作。
@BindingAdapter("imageUrl")
public static void setImageUrl(ImageView imageView, String url) {
Log.d("TEST","URL: " + url);
}
Above code shows how it is implemented in my ViewModel.上面的代码显示了它是如何在我的 ViewModel 中实现的。 Nothing special.
没什么特别的。
<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"/>
This does not work.这不起作用。 namespace app is unbound.
命名空间应用程序未绑定。 So what am i missing.
所以我错过了什么。 I tried following https://medium.com/google-developers/android-data-binding-custom-setters-55a25a7aea47#.6ygaiwooh and see how they set bindingAdapter.
我尝试关注https://medium.com/google-developers/android-data-binding-custom-setters-55a25a7aea47#.6ygaiwooh并查看他们如何设置 bindingAdapter。 But there is something i have missed
但是我错过了一些东西
我遇到了同样的问题,我错过了使用以下方法绑定布局:
DataBindingUtil.setContentView(activity, layoutResId);
I suggest to use "bind" namespace for bindable attributes and use same names for adapter parameter and layout attribute.我建议对可绑定属性使用“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"/>
where namespace "app" was replaced to "bind" .其中命名空间"app"被替换为"bind" 。 On your Layout root:
在您的布局根目录上:
xmlns:bind="http://schemas.android.com/apk/res-auto"
Remember to add the following line to your app's build.gradle
file:请记住
build.gradle
添加到您应用的build.gradle
文件中:
apply plugin: 'kotlin-kapt'
and check @BindingAdapter syntax:并检查@BindingAdapter 语法:
@BindingAdapter("visibleOnScreen")
fun View.setVisibility(isVisible: ObservableBoolean) {
if (isVisible.get())
this.visibility = View.VISIBLE
else
this.visibility = View.GONE
}
in view's xml:在视图的 xml 中:
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:visibleOnScreen="@{viewModel.errorOccurred}" />
viewModel:视图模型:
var errorOccurred = androidx.databinding.ObservableBoolean(false)
Add the following line to your app's build.Gradle file:将以下行添加到您应用的 build.Gradle 文件中:
apply plugin: 'kotlin-kapt'
and enable data-binding in the android block in the app's build.Gradle file:并在应用程序的 build.Gradle 文件的 android 块中启用数据绑定:
android {
.
.
buildFeatures {
dataBinding true
}
.
.
}
create BindingAdapters.kt file and add @BindingAdapter with below syntax:创建 BindingAdapters.kt 文件并使用以下语法添加 @BindingAdapter:
@BindingAdapter("imageUrl")
fun ImageView.bindImage(imgUrl: String){
Glide.with(context)
.load(imgUrl)
.into(this)
}
in view's xml use imageUrl='@{"YourImageUrl"}' syntax in the ImageView:在视图的 xml 中,在 ImageView 中使用 imageUrl='@{"YourImageUrl"}' 语法:
<ImageView
android:id="@+id/imageView3"
android:layout_width="100dp"
android:layout_height="100dp"
android:background="@drawable/img"
imageUrl='@{"YourImageUrl"}'/>
This works well ;)这很好用;)
My project Gradle version: 7.0.0我的项目 Gradle 版本:7.0.0
I just placed buildFeatures block after compileOptions and kotlinOptions in android block of build.gradle(:app).我只是在 build.gradle(:app) 的 android 块中的 compileOptions 和 kotlinOptions 之后放置了 buildFeatures 块。 It worked
有效
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
kotlinOptions {
jvmTarget = '1.8'
}
buildFeatures {
databinding true
}
I had same issue and none of the other solutions worked for me.我有同样的问题,其他解决方案都不适合我。 Then this worked.
然后这奏效了。
In activity活动中
dataBinding.lifecycleOwner = this
试试看
@BindingAdapter({"bind:imageUrl"})
You are using app:imageUrl="@{item.imageUrl}"
in the xml, make sure you have a String called imageUrl
in your model.您在 xml 中使用
app:imageUrl="@{item.imageUrl}"
,请确保您的模型中有一个名为imageUrl
的字符串。
You must pass the url link of the image and not the binderAdapter's name.您必须传递图像的 url 链接,而不是 binderAdapter 的名称。
Syntax :句法 :
app:BinderAdapterName="@{item.imagelink}"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.