Android Data Binding different icon based on different states

Viewed 183

Iv'e got a recyclerView with items using databinding. The model contains multiple values that together determine the icon/state of the list item.

so i tried to add a variable to the XML that contains a helper function to return a R.drawable that needs to be shown based on different values in the model

ListItem.xml

<data>
  <import type="android.view.View"/>
  <variable name="model" type="com.example.model"></variable>
  <variable name="helper" type="com.example.helper"></variable>
</data>

...

<TextView ...
  android:text="@{model.a}" /> <<< THIS UPDATES LIKE EXPECTED

<ImageView ...
  app:imageResource="@{helper.getIcon(model)}"/> <<< THIS IS NOT UPDATING AFTER THE VALUES CHANGES

...

helper.kt

fun getIcon(model:Model){
 return if(model.a == true){
   if(model.b == true){
     R.drawable.b
   }else{
     R.drawable.a
   }
 }else{
    R.drawable.c
 }
}

if the model values changes the getIcon is not triggered. (only when i scroll up and down)

0 Answers
Related