如果使用Glide存在,则如何设置配置文件图片,否则请使用占位符?

发布于 2025-01-21 19:10:22 字数 899 浏览 5 评论 0原文

我创建了一个盲目的适配器:

@BindingAdapter("android:profileImage")
fun setProfileImage(imageView: ImageView, profileImage: String) {
    Glide.with(imageView.context).load(profileImage)
        .placeholder(R.drawable.placeholder)
        .into(imageView)
}

它可以很好地解决。但是,当我从公共API中请求用户时,在某些情况下,profileImageImage为null。我的目标是当profileImageImage不是null时使用Glide,否则请使用可绘制目录中存在的占位符。这是我尝试使用的图像视图:

android:profileImage="@{user.isProfilePictureNotNull() ? user.profileImage : @drawable.placeholder}"

iSprofilepicturenotnull()定义为:

fun isProfilePictureNotNull() = profileImage != null

但是Android Studio反对:

'!=', '%', '*', '+', '-', '/', ':', <, <<, <=, '==', '>', '>=', '>>', '>>>' or '[' expected, got ':'.

如何解决此问题?

I have created a blinding adapter:

@BindingAdapter("android:profileImage")
fun setProfileImage(imageView: ImageView, profileImage: String) {
    Glide.with(imageView.context).load(profileImage)
        .placeholder(R.drawable.placeholder)
        .into(imageView)
}

And it woks fine. However, when I request the users from a public API, there are some cases in which the profileImage is null. My goal is to use Glide when the profileImage is not null, otherwise use the placeholder that exists in the drawable directory. This what I have tried to my ImageVIew:

android:profileImage="@{user.isProfilePictureNotNull() ? user.profileImage : @drawable.placeholder}"

Where isProfilePictureNotNull() is defined as:

fun isProfilePictureNotNull() = profileImage != null

But Android Studio is complaning with:

'!=', '%', '*', '+', '-', '/', ':', <, <<, <=, '==', '>', '>=', '>>', '>>>' or '[' expected, got ':'.

How to solve this issue?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

抽个烟儿 2025-01-28 19:10:22

这里的问题是,您正在尝试将不是字符串(@drawable.placeholder)的东西传递给绑定适配器。如果将其替换为字符串,您将看到错误解决。

我的建议是不是在装订适配器之外进行逻辑,为什么不在里面呢?类似的事情:

    @BindingAdapter("android:profileImage")
fun setProfileImage(imageView: ImageView, profileImage: String?) {
    if (profileImage != null) {
        Glide.with(imageView.context).load(profileImage)
            .placeholder(R.drawable.placeholder)
            .into(imageView)
    } else {
        imageView.setImageDrawable(R.drawable.placeholder)
    }
}

现在,您的XML声明可以简单地说:

android:profileImage="@{user.profileImage}"

此外,Glide具有.Error()调用您可以将其放在构建器上。我不确定它是否可以与Null一起使用或触发它,但是您可以试一试。然后看起来像这样:

        @BindingAdapter("android:profileImage")
fun setProfileImage(imageView: ImageView, profileImage: String?) {
    Glide.with(imageView.context).load(profileImage)
        .placeholder(R.drawable.placeholder)
        .error(R.drawable.placeholder)
        .into(imageView)
}

The issue here is that you're attempting to pass something which is not a String (@drawable.placeholder) to your binding adapter. If you replace it with a String, you will see that the error resolves.

My advice would be instead of doing the logic outside of the binding adapter, why not do it inside? Something like this:

    @BindingAdapter("android:profileImage")
fun setProfileImage(imageView: ImageView, profileImage: String?) {
    if (profileImage != null) {
        Glide.with(imageView.context).load(profileImage)
            .placeholder(R.drawable.placeholder)
            .into(imageView)
    } else {
        imageView.setImageDrawable(R.drawable.placeholder)
    }
}

Now, your XML declaration can simply be:

android:profileImage="@{user.profileImage}"

Additionally, Glide has a .error() call you can put onto the builder. I am not certain if it works with null or what triggers it, but you could give it a shot. It would look something like this, then:

        @BindingAdapter("android:profileImage")
fun setProfileImage(imageView: ImageView, profileImage: String?) {
    Glide.with(imageView.context).load(profileImage)
        .placeholder(R.drawable.placeholder)
        .error(R.drawable.placeholder)
        .into(imageView)
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文