Set drawable resource ID in android:src for ImageView using data binding in Android

后端 未结 15 2004
陌清茗
陌清茗 2020-12-04 09:27

I\'m trying to set drawable resource ID to android:src of ImageView using data binding

Here is my object:

public class Recipe implem         


        
相关标签:
15条回答
  • 2020-12-04 10:10

    This work for me. i would have add it to @hqzxzwb answer as comment but due to reputation limitations.

    I have this in my view Model

    var passport = R.drawable.passport
    

    Then in my xml, I have

    android:src="@{context.getDrawable(model.passort)}"
    

    And thats it

    0 讨论(0)
  • 2020-12-04 10:15

    There is no need for a custom BindingAdapter at all. Just use

    app:imageResource="@{yourResId}"
    

    and it will work fine.

    Check this for how it works.

    0 讨论(0)
  • 2020-12-04 10:18

    Never override standard SDK attributes when you create your own @BindingAdapter!

    This is not a good approach for many reasons like: it's gonna prevent obtaining benefits of new fixes on Android SDK update on that attribute. Also it might confuse developers and surely tricky for reusability (because it's un-exptected to be overrided )

    you may use different namespace like:

    custom:src="@{recipe.imageResource}"
    

    or

    mybind:src="@{recipe.imageResource}"
    

    ------ start Update 2.Jul.2018

    Namespace is not recommended to be used, so better to rely on prefix or different name as:

    app:custom_src="@{recipe.imageResource}"
    

    or

    app:customSrc="@{recipe.imageResource}"
    

    ------ end Update 2.Jul.2018

    However, I would recommend different solution as:

    android:src="@{ContextCompat.getDrawable(context, recipe.imageResource)}"
    

    context view is always available inside binding expression @{ ... }

    0 讨论(0)
  • 2020-12-04 10:19
    <?xml version="1.0" encoding="utf-8"?>
    <layout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools">
        <data>
            <variable
               name="model"
               type="YourViewModel"/>
        </data>
    
        <androidx.constraintlayout.widget.ConstraintLayout
              android:layout_width="match_parent"
              android:layout_height="wrap_content"
              android:background="?android:attr/selectableItemBackground"
              android:paddingStart="@dimen/dp16"
              android:paddingTop="@dimen/dp8"
              android:paddingEnd="@dimen/dp8"
              android:paddingBottom="@dimen/dp8">
    
              <ImageView
                  android:layout_width="wrap_content"
                  android:layout_height="wrap_content" 
                  android:src="@{model.selected ? @drawable/check_fill : @drawable/check_empty}"/>
    
    </androidx.constraintlayout.widget.ConstraintLayout>
    </layout>
    
    0 讨论(0)
  • 2020-12-04 10:20

    you can do the following

    android:src="@{expand?@drawable/ic_collapse:@drawable/ic_expand}"
    
    0 讨论(0)
  • 2020-12-04 10:21

    Building upon the answer from Maher Abuthraa, this is what I ended up using in the XML:

    android:src="@{context.getDrawable(recipe.imageResource)}"
    

    The context variable is available in binding expression without any imports. Also, no custom BindingAdapter necessary. Only caveat: the method getDrawable is only available since API 21.

    0 讨论(0)
提交回复
热议问题