[英]The inflate method for my binding is not found (using Android, Data Binding.)
I'm using data binding to bind the layouts in my Android app. 我正在使用数据绑定来绑定我的Android应用程序中的布局。
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!). 我已经设置了我的布局(my_custom.xml)并生成了绑定类(MyCustomBinding),但Android Studio似乎没有立即找到Binding类的.inflate(...)方法,将其标记为错误(红色文字!)。
The code seems to be correct though, since it compiles and builds just fine into an APK. 代码似乎是正确的,因为它编译和构建很好的APK。
How do I get Android Studio to update correctly ? 如何让Android Studio正确更新?
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>
Something is not completing in Android Studio because you haven't actually implemented data binding. 有些东西没有在Android Studio中完成,因为您实际上没有实现数据绑定。 Once you add a variable to your layout's data
element, the inflate
method will be found as you expect. 将变量添加到布局的data
元素后,将按预期找到inflate
方法。 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: 例如:
create a View Model: 创建一个视图模型:
public class MyViewModel {
public final ObservableField<String> name;
public MyViewModel(String name) {
this.name = new ObservableField<>(name);
}
}
and use it in your layout 并在您的布局中使用它
<?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>
(note the variable
declared in the data
element, and how it is referenced in the TextView's text
attribute) (注意data
元素中声明的variable
,以及它在TextView的text
属性中的引用方式)
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);
}
}
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 ) 当然,通过自定义视图类中的setter传入数据或者甚至从容器视图传入数据可能会更好(请参阅http://developer.android.com/tools/data-binding/ guide.html #include )
You can try setting a custom class name to your Binding Layout, and referencing it in your custom view to make it clear which layout you using: 您可以尝试为绑定布局设置自定义类名,并在自定义视图中引用它,以明确您使用的布局:
<layout>
<data class="MyCustomBinding">
</data>
</layout>
If this does not work, use DataBindingUtil
instead of MyCustomBinding
, which returns the base DataBinding class and has a inflate()
method: 如果这不起作用,请使用DataBindingUtil
而不是MyCustomBinding
,它返回基本DataBinding类并具有inflate()
方法:
MyCustomBinding binding = DataBindingUtil.inflate(inflater, this, true);
Sometimes the binding cannot be known in advance. 有时绑定不能提前知道。 In such cases, the binding can be created using the DataBindingUtil class: 在这种情况下,可以使用DataBindingUtil类创建绑定:
ViewDataBinding binding = DataBindingUtil.inflate(LayoutInflater, layoutId,
parent, attachToParent);
ViewDataBinding binding = DataBindingUtil.bindTo(viewRoot, layoutId);
You dont need that type of stuffs, DataBinding includes DataBindingUtils class here we go. 你不需要那种类型的东西,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.