Issue
I can pass a resource to layout by binding a method call which returns the drawable int (R.drawable).
I want to do it by binding the variable itself onto the layout.
ListFragment.java
public void bindVariables(ListViewBinding binding) {
// trying to bind this variable onto layout
int drawableInt = 0;
binding.setVariable(drawableInt, R.drawable.item1Image);
binding.setmyService(this.myService);
}
ItemInList.xml
<data>
<variable name="drawbleInt" type="java.lang.Integer"/>
<variable name="myService" type="com.myService"/>
</data>
**// trying to use the variable set in fragment **
<ImageView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
app:srcVector="@{ drawableInt}"/>
**//this works - using service variable**
<ImageView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
app:srcVector="@{ myService.drawableInt()}"/>
app:srcVector is a @bindingAdapter to set drawable image to the view.
DataBindingAdapter class
@BindingAdapter("app:srcVector")
public static void setSrcVector(ImageView view, @DrawableRes int drawable){
view.setImageResource(drawable);
}
Solution
You're setting the variable with id 0, but this does not identify the variable you want to set.
int drawableInt = 0;
binding.setVariable(drawableInt, R.drawable.item1Image);
Instead you'd have to use setDrawableInt()
or use the correct id of the variable BR.drawableInt
.
binding.setDrawableInt(R.drawable.item1Image);
binding.setVariable(BR.drawableInt, R.drawable.item1Image);
Answered By - tynn
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.