0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

CustomDatabindingAdapterでURLがあったら表示、なかったらデフォルトの画像を表示

0
Last updated at Posted at 2018-04-24

例えばimageUrlがあったらそれを表示してなかったらデフォルトの画像を表示したい場合

CustonBindingAdapter.kt
object CustomBindingAdapter {
    @JvmStatic
    @BindingAdapter("app:imageUrl")
    fun imageUrl(imageView: ImageView, imageUrl: String?) {
        if (imageUrl != null)
            Glide.with(imageView.context).load(imageUrl).into(imageView)
        else
            imageView.setImageResource(R.drawable.no_image)
    }
}

これでxmlからapp:imageUrlでアクセスできる

<ImageView
    android:id="@+id/hospital_image_view"
    android:layout_width="80dp"
    android:layout_height="80dp"
    android:layout_margin="10dp"
    app:imageUrl="@{ hospital.mainImage }"/>
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?