I have an ImageView for which I wanted to write a custom binding adapter
<ImageView
android:id="@+id/item_list_picture"
android:layout_height="@dimen/normal_475"
android:layout_alignParentEnd="true"
android:layout_alignParentTop="true"
android:layout_above="@id/item_list_secondary_text"
android:contentDescription="@{listItemViewModel.primaryText}"
android:padding="@dimen/normal_100"
android:displayImageUrl="@{listItemViewModel.pictureUrl}"
android:placeHolderDrawable="@{@drawable/ic_baseline_image_24}"
android:layout_width="@dimen/normal_475"
android:specificWidth="@dimen/normal_475"
android:goneUnless="@{listItemViewModel.pictureUrl != null}" />
The dimension for specificWidth
is defined in dimens.xml
as
<dimen name="normal_475">77dp</dimen>
And finally the BindingAdapter.kt
file has the following code:
@BindingAdapter("android:displayImageUrl", "android:placeHolderDrawable", "android:specificWidth")
fun loadImage(view: ImageView, displayImageUrl: String?, placeHolderDrawable: Drawable, @DimenRes width: Int) {
if (!displayImageUrl.isNullOrEmpty()) {
Picasso.get().load(displayImageUrl).placeholder(placeHolderDrawable)
.resize(view.context.resources.getDimensionPixelSize(width), view.context.resources.getDimensionPixelSize(width)).centerCrop().into(view)
} else {
view.setImageDrawable(placeHolderDrawable)
}
}
If I use the binding adapter's loadImage
without the specificWidth
everything works. Otherwise, I get the following error
ERROR:C:\dev\projects\myapp\app\src\main\res\layout\item_list.xml:69: AAPT: error: attribute android:specificWidth not found.
What is the problem here? How can I get the specific width from the dimension defined? I also tried to add android:layout_width
to the binding adapter instead of my custom android:specificWidth
, but it doesn't work either. The possibility to use android:layout_width
value in dp would be even better as I need to get the size given for the ImageView
to resize the downloaded image from pictureUrl
.
Because android:specificWidth
's type is resource you need to set it like below:
android:specificWidth="@{R.dimen.normal_475}"
also you need to import R
in your data tag:
<import type="your.package.name.R" />
Or if you want to pass the dimen value you need to change your bindingAdapter
specificWidth
input to this:
fun loadImage(
view: ImageView,
displayImageUrl: String?,
placeHolderDrawable: Drawable,
width: Float // change the type to float and clear the `@dimenRes` annotation
also change your xml to:
android:specificWidth="@{@dimen/normal_475}" // you need to pass the value in data-binding format
By the way you can also ignore this android:specificWidth
attribute as you said and instead read the value from the view's width and height. you just need to do it on view.post{}
to make sure the view is rendered and its size is valid. so your bindingAdapter
fun would be something like this:
@BindingAdapter("android:displayImageUrl", "android:placeHolderDrawable")
fun loadImage(view: ImageView, displayImageUrl: String?, placeHolderDrawable: Drawable) {
view.post {
if (!displayImageUrl.isNullOrEmpty()) {
Picasso.get().load(displayImageUrl).placeholder(placeHolderDrawable)
.resize(
view.width,
view.height
).centerCrop().into(view)
} else {
view.setImageDrawable(placeHolderDrawable)
}
}
}
If you had any questions or I didn't get your problem well please don't hesitate to ask.
The solution that worked for me was to define the specific width as android:specificWidth="@{@dimen/normal_475}"
.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.