簡體   English   中英

Android中維度資源的綁定適配器

[英]Binding adapter for dimension resource in Android

我有一個 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}" />

specificWidth的尺寸在dimens.xml中定義為

<dimen name="normal_475">77dp</dimen>

最后, 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)
    }
}

如果我使用沒有specificWidth的綁定適配器的loadImage ,一切正常。 否則,我會收到以下錯誤

ERROR:C:\dev\projects\myapp\app\src\main\res\layout\item_list.xml:69: AAPT: error: attribute android:specificWidth not found.

這里有什么問題? 如何從定義的尺寸中獲取特定寬度? 我還嘗試將android:layout_width添加到綁定適配器而不是我的自定義android:specificWidth ,但它也不起作用。 在 dp 中使用android:layout_width值的可能性會更好,因為我需要獲得ImageView的大小以調整從pictureUrl下載的圖像的大小。

因為android:specificWidth的類型是資源,你需要像下面這樣設置它:

android:specificWidth="@{R.dimen.normal_475}"

您還需要在數據標簽中導入R

<import type="your.package.name.R" />

或者,如果您想傳遞 dimen 值,您需要將bindingAdapter specificWidth輸入更改為此:

fun loadImage(
view: ImageView,
displayImageUrl: String?,
placeHolderDrawable: Drawable,
width: Float // change the type to float and clear the `@dimenRes` annotation

還將您的 xml 更改為:

 android:specificWidth="@{@dimen/normal_475}" // you need to pass the value in data-binding format

順便說一下,您也可以像您所說的那樣忽略這個android:specificWidth屬性,而是從視圖的寬度和高度中讀取值。 您只需要在view.post{}上執行此操作以確保呈現視圖並且其大小有效。 所以你的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)
        }
    }
}

如果您有任何問題,或者我沒有很好地解決您的問題,請隨時提出。

對我有用的解決方案是將特定寬度定義為android:specificWidth="@{@dimen/normal_475}"

暫無
暫無

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

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