How to access instance variable inside BindingAdapter when using Android data binding?

So, I use this popular piece of data binding code to load an image into the figurative representation of list items by going to the URL:

<ImageView android:layout_width="match_parent" android:layout_height="150dp"" app:imageUrl="@{movie.imageUrl}" /> 

Binding Adapter:

 class Movie{ boolean isLoaded; @BindingAdapter({"bind:imageUrl"}) public static void loadImage(final ImageView view, String imageUrl) { Picasso.with(view.getContext()) .load(imageUrl) .into(view, new Callback.EmptyCallback() { @Override public void onSuccess() { //set isLoaded to true for the listview item // but cannot access boolean isLoaded as it is non static. }); } 

If I just make the BindingAdapter non-static, then it throws an error:

 java.lang.IllegalStateException: Required DataBindingComponent is null in class MovieTileBinding. A BindingAdapter in com.example.moviesapp.Pojos.Results is not static and requires an object to use, retrieved from the DataBindingComponent. If you don't use an inflation method taking a DataBindingComponent, use DataBindingUtil.setDefaultComponent or make all BindingAdapter methods static. 
+5
source share
1 answer

Put the entire movie through a custom biding adapter:

  <ImageView android:layout_width="match_parent" android:layout_height="150dp"" app:movie="@{movie}" /> 

Binding Adapter:

 class Movie{ boolean isLoaded; @BindingAdapter({"movie"}) public static void loadImage(final ImageView view, final Movie movie) { if(movie != null && moview.getImageUrl() != null){ Picasso.with(view.getContext()) .load(movie.getImageUrl()) .into(view, new Callback.EmptyCallback() { @Override public void onSuccess() { image.setLoaded(true); }); } } 
+1
source

Source: https://habr.com/ru/post/1236751/


All Articles