6

我想将视图引用指定为属性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);
}

但是我的进度视图为空。我尝试将上下文转换为活动,并且findViewById还为空另一个解决方案只是在图像下方添加进度视图,当它成功加载时,它应该被覆盖 bt 图像

4

3 回答 3

11

奇怪的是你看到一个空的progressView。您的布局中可能没有它?

在任何情况下,您都可以这样做:

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

在您的 BindingAdapter 中:

@BindingAdapter(value = {"imageUrl", "progressView"}, requireAll = false)
public static void setImageUrl(ImageView imageView, String url, ProgressBar progressBar) {
    ...
}
于 2018-01-03T19:00:51.410 回答
0

在我的示例中,我将 ScrollView id 作为 BindingAdapter 的参数传递。

这是布局代码:

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

这是用 Kotlin 编写的 Bindig Adapters 代码:

@JvmStatic @BindingAdapter(value = ["fadeVisible", "fadeScrollView"], requireAll = false)
fun setFadeVisible(view: View, visible: Boolean, scrollView: ScrollView) {
...
于 2018-06-01T09:21:06.867 回答
0

@BindingAdapter "value" 是一个字符串数组,所以试试这个:

@BindingAdapter(value = {"imageUrl", "progressViewId"}, requireAll = false)
public static void setImageUrl(ImageView imageView, String iamgeUrl, String progressViewStrId) {
     int progressBarId = Integer.valueOf(progressViewStrId);
     ...
}
于 2018-01-03T10:22:44.590 回答