6

I want to specify view reference as atribute to ImageView

@BindingAdapter(value = {"imageUrl", "progressView"}, requireAll = false)
public static void setImageUrl(ImageView imageView, String url, @IdRes int progressBar) {
    Context context = imageView.getContext();
    View progressView  = imageView.getRootView().findViewById(progressBar);
     progressView.setVisibility(View.VISIBLE);

    Glide.with(context).load(url).listener(new RequestListener<String, GlideDrawable>() {
        @Override
        public boolean onException(Exception e, String model, Target<GlideDrawable> target, boolean isFirstResource) {
            if (progressView!= null) {
                 progressView.setVisibility.setVisibility(View.GONE);
            }
            return false;
        }

        @Override
        public boolean onResourceReady(GlideDrawable resource, String model, Target<GlideDrawable> target, boolean isFromMemoryCache, boolean isFirstResource) {
            if (progressView!= null) {
                 progressView.setVisibility(View.GONE);
            }
            return false;
        }
    }).crossFade().into(imageView);
}

however my progress view is null. I tried cast context to activity and findViewById also null Another solution is just add progress view below Image and when it loads successfully it should be overlayed bt image

Chickin Nick
  • 517
  • 1
  • 6
  • 21

3 Answers3

13

Strange that you're seeing a null progressView. You may not have it in your layout maybe?

In any case, you can do this instead:

<ProgressBar android:id="@+id/progressBarView" .../>
<!-- other stuff -->
<ImageView app:progressView="@{progressBarView}" app:imageUrl="..." .../>

And in your BindingAdapter:

@BindingAdapter(value = {"imageUrl", "progressView"}, requireAll = false)
public static void setImageUrl(ImageView imageView, String url, ProgressBar progressBar) {
    ...
}
George Mount
  • 20,708
  • 2
  • 73
  • 61
  • 2
    This is a great answer. I did have an issue with using progress_bar_view for the id and trying to put that into the app:progressView, but when databinding occurs, id's turn into camelcase. – Carson J. Feb 13 '19 at 02:15
0

@BindingAdapter "value" is a array of Strings, so try this:

@BindingAdapter(value = {"imageUrl", "progressViewId"}, requireAll = false)
public static void setImageUrl(ImageView imageView, String iamgeUrl, String progressViewStrId) {
     int progressBarId = Integer.valueOf(progressViewStrId);
     ...
}
Oleg Sokolov
  • 1,134
  • 1
  • 12
  • 19
0

In my example I pass a ScrollView id as parameter for the BindingAdapter.

This is the layout code:

<LinearLayout
    android:orientation="horizontal"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    app:fadeVisible="@{viewModel.shippingMethodsVisible}"
    app:fadeScrollView="@{scrollView}">

And this is the Bindig Adapters code written in Kotlin:

@JvmStatic @BindingAdapter(value = ["fadeVisible", "fadeScrollView"], requireAll = false)
fun setFadeVisible(view: View, visible: Boolean, scrollView: ScrollView) {
...
Jose
  • 1
  • 3