使用DataBindingComponent的膨胀方法 [英] Inflate method using DataBindingComponent

查看:328
本文介绍了使用DataBindingComponent的膨胀方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

一旦Glide成功渲染了图像,我在更新textview时会看到此错误.

I see this error while updating a textview once the image has been successfully rendered by Glide.

致命异常:java.lang.IllegalStateException:必需 DataBindingComponent在类CustomBinding中为null.绑定适配器 在CustomViewModel中,它不是静态的,需要使用一个对象, 从DataBindingComponent检索.如果您不使用通货膨胀 使用DataBindingComponent的方法,使用 DataBindingUtil.setDefaultComponent或使所有BindingAdapter方法 静态的.

Fatal Exception: java.lang.IllegalStateException: Required DataBindingComponent is null in class CustomBinding. A BindingAdapter in CustomViewModel 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.

@BindingAdapter(value = { "android:src", "placeHolder" }, requireAll = false) 
public void setUrl(ImageView imageView, String url, Drawable placeHolder) {
          Glide.with(imageView.getContext())
              .load(url)
              .placeholder(placeHolder)
              .centerCrop()
              .listener(new Listener<String, Drawable>() {
                @Override
                public boolean onException() {
                  viewmodel.setTextVisible(true);// ERROR!
                  return false;
                }

                @Override public boolean onResourceReady() {
                  viewmodel.setTextVisible(false); // ERROR!
                  return false;
                }
              })
              .into(imageView);
        }

public void setTextVisible(boolean visibility) {
    textVisibility = visibility;
    notifyPropertyChanged(BR.textVisibility);
}

    @Bindable 
    public boolean getTextVisible() {
      return textVisibility; 
    }

这是我初始化视图模型并将数据绑定到片段内的方式:

This is how i initialise the viewmodel and bind the data inside the fragment:

CustomBinding binding =
        DataBindingUtil.inflate(inflater, R.layout.custom, container, 
false);

    CustomViewModel viewModel = new CustomViewModel(data, context);
    binding.setHandlers(new CustomHandlers(context));
    binding.setData(viewModel);

我找不到在ViewModel中实际实现此方法的方法.预先感谢您的帮助.

I cant find a way to actually implement this within a viewmodel. Thanks in advance for the help.

推荐答案

面对相同的问题,可以通过在方法中添加@JvmStatic批注来解决此问题,并且可以正常工作.

Was facing the same issue, got resolved by adding @JvmStatic annotation to the method and it works.

应采用以下形式

@BindingAdapter("app:someAttribute")

@BindingAdapter("app:someAttribute")

@JvmStatic

@JvmStatic

示例

class TestBindingHelper {

    companion object{

        @BindingAdapter("app:serviceOptions")
        @JvmStatic
        fun setServiceOptions(recyclerView: RecyclerView, listOfData: List<String>?) {
             //do something funny
        }

    }
}

这篇关于使用DataBindingComponent的膨胀方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆