[英]Binding adapter for dimension resource in Android
I have an ImageView for which I wanted to write a custom binding adapter我有一个 ImageView,我想为其编写一个自定义绑定适配器
<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 specificWidth
的尺寸在dimens.xml
中定义为
<dimen name="normal_475">77dp</dimen>
And finally the BindingAdapter.kt
file has the following code:最后,
BindingAdapter.kt
文件包含以下代码:
@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.如果我使用没有
specificWidth
的绑定适配器的loadImage
,一切正常。 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.我还尝试将
android:layout_width
添加到绑定适配器而不是我的自定义android:specificWidth
,但它也不起作用。 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
.在 dp 中使用
android:layout_width
值的可能性会更好,因为我需要获得ImageView
的大小以调整从pictureUrl
下载的图像的大小。
Because android:specificWidth
's type is resource you need to set it like below:因为
android:specificWidth
的类型是资源,你需要像下面这样设置它:
android:specificWidth="@{R.dimen.normal_475}"
also you need to import R
in your data tag:您还需要在数据标签中导入
R
:
<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:或者,如果您想传递 dimen 值,您需要将
bindingAdapter
specificWidth
输入更改为此:
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:还将您的 xml 更改为:
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.顺便说一下,您也可以像您所说的那样忽略这个
android:specificWidth
属性,而是从视图的宽度和高度中读取值。 you just need to do it on view.post{}
to make sure the view is rendered and its size is valid.您只需要在
view.post{}
上执行此操作以确保呈现视图并且其大小有效。 so your bindingAdapter
fun would be something like this:所以你的
bindingAdapter
乐趣将是这样的:
@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}"
.对我有用的解决方案是将特定宽度定义为
android:specificWidth="@{@dimen/normal_475}"
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.