繁体   English   中英

找不到我的绑定的inflate方法(使用Android,Data Binding。)

[英]The inflate method for my binding is not found (using Android, Data Binding.)

我正在使用数据绑定来绑定我的Android应用程序中的布局。

我已经设置了我的布局(my_custom.xml)并生成了绑定类(MyCustomBinding),但Android Studio似乎没有立即找到Binding类的.inflate(...)方法,将其标记为错误(红色文字!)。

代码似乎是正确的,因为它编译和构建很好的APK。

如何让Android Studio正确更新?

代码示例:

这是我的自定义查看代码:

    public class MyCustomView extends FrameLayout {
    public MyCustomView(Context context) {
        this(context, null, 0);
    }
    public MyCustomView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }
    public MyCustomView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        MyCustomBinding binding = MyCustomBinding.inflate(inflater, this, true);
        binding.aButton.setText("Whatever");
    }
}

布局定义为:

    <?xml version="1.0" encoding="utf-8"?>
<layout
    xmlns:android="http://schemas.android.com/apk/res/android">
    <data>
    </data>
    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        >
        <TextView
            android:id="@+id/a_button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Click me!"
            android:padding="10dp"
            android:background="#000"
            android:textColor="#fff"
            android:layout_gravity="center"
            />
    </FrameLayout>
</layout>

这是问题所在:(突出显示为红色)

在此输入图像描述

有些东西没有在Android Studio中完成,因为您实际上没有实现数据绑定。 将变量添加到布局的data元素后,将按预期找到inflate方法。 也就是说,通过直接通过绑定设置文本字段的值,您实际上没有获得数据绑定的好处。 您应该在绑定中设置视图模型,然后让绑定相应地更新视图。 例如:

创建一个视图模型:

public class MyViewModel {
    public final ObservableField<String> name;
    public MyViewModel(String name) {
        this.name = new ObservableField<>(name);
    }
}

并在您的布局中使用它

<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android">
    <data>
        <variable name="model" type="com.victoriaschocolates.conceirge.MyViewModel" />
    </data>
    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        >
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@{model.name}"
            android:padding="10dp"
            android:background="#000"
            android:textColor="#fff"
            android:layout_gravity="center"
            />
    </FrameLayout>
</layout>

(注意data元素中声明的variable ,以及它在TextView的text属性中的引用方式)

然后在自定义视图中绑定两个:

public class MyCustomView extends FrameLayout {

    public MyCustomView(Context context) {
        this(context, null, 0);
    }

    public MyCustomView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public MyCustomView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        MyCustomBinding binding = MyCustomBinding.inflate(inflater, this, true);
        MyViewModel model = new MyViewModel("Whatever");
        binding.setModel(model);
    }
}

当然,通过自定义视图类中的setter传入数据或者甚至从容器视图传入数据可能会更好(请参阅http://developer.android.com/tools/data-binding/ guide.html #include

您可以尝试为绑定布局设置自定义类名,并在自定义视图中引用它,以明确您使用的布局:

<layout>
    <data class="MyCustomBinding">
    </data>
</layout>

如果这不起作用,请使用DataBindingUtil而不是MyCustomBinding ,它返回基本DataBinding类并具有inflate()方法:

MyCustomBinding binding = DataBindingUtil.inflate(inflater, this, true);

来自文档

有时绑定不能提前知道。 在这种情况下,可以使用DataBindingUtil类创建绑定:

ViewDataBinding binding = DataBindingUtil.inflate(LayoutInflater, layoutId,
parent, attachToParent);
ViewDataBinding binding = DataBindingUtil.bindTo(viewRoot, layoutId);

你不需要那种类型的东西,DataBinding包含DataBindingUtils类。

public MyCustomView(Context context, AttributeSet attrs, int defStyle){
    super(context, attrs, defStyle);
    LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    MyCustomBinding binding = DataBindingUtil.inflate(inflater, this, true);
    binding.aButton.setText("Whatever");
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM