找不到用于我的绑定的inflate方法(使用Android,数据绑定.) [英] The inflate method for my binding is not found (using Android, Data Binding.)
问题描述
我正在使用数据绑定来绑定Android应用程序中的布局.
I'm using data binding to bind the layouts in my Android app.
我已经设置好布局(my_custom.xml)并生成了绑定类(MyCustomBinding),但是Android Studio似乎没有立即找到Binding类的.inflate(...)方法,将其标记出来作为错误(红色文本!).
I have set up my layout ( my_custom.xml ) and the binding class is generated (MyCustomBinding), but Android Studio does not seem to find the .inflate(...) method of the Binding class right away, marking it as an error ( red text!).
代码似乎是正确的,因为它可以编译并很好地构建为APK.
The code seems to be correct though, since it compiles and builds just fine into an APK.
如何使Android Studio正确更新?
How do I get Android Studio to update correctly ?
这是我的自定义查看代码:
This is my custom View code:
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
方法.就是说,通过绑定直接设置文本字段的值,您实际上没有从数据绑定中获得好处.您应该在绑定中设置视图模型,然后让绑定相应地更新视图.例如:
Something is not completing in Android Studio because you haven't actually implemented data binding.
Once you add a variable to your layout's data
element, the inflate
method will be found as you expect. That said, you're really not getting the benefit of databinding by setting the value of the text field directly through the binding. You should instead be setting a View Model in your binding, and then let the binding update the views accordingly. For example:
创建视图模型:
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
属性中对其进行引用)
(note the variable
declared in the data
element, and how it is referenced in the TextView's text
attribute)
然后将两者绑定到您的自定义视图中:
then bind the two in your custom view:
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传递数据,或者甚至从容器视图中传递数据(请参见
Of course, it would probably be better still to have the data passed in through a setter in the custom view class, or even passed in from the container view (see http://developer.android.com/tools/data-binding/guide.html#includes)
这篇关于找不到用于我的绑定的inflate方法(使用Android,数据绑定.)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!