简体   繁体   English

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

[英]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正确更新?

Code example: 代码示例:

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");
    }
}

layout is defined as: 布局定义为:

    <?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>

And here's the issue: (highlighted red) 这是问题所在:(突出显示为红色)

在此输入图像描述

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);

From the docs : 来自文档

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.

相关问题 安卓数据绑定。 按钮 onClick 不起作用 - Android Data Binding. Button onClick not working Android 视图绑定。 如何在 Basic Activity/Fragment 中实现绑定? - Android view binding. How implement binding in Basic Activity/Fragment? android数据绑定。 如何通过包含布局内的ID访问视图 - android data binding. How to access view via ID inside a included layout 为包含数据绑定的Viewstub充气 - Inflate a Viewstub that contains a Data Binding ViewModel + Data Binding中的最佳实践和模式。 ViewModel中的ObservableField好吗? - Best practices and patterns in ViewModel + Data Binding. Is ObservableField in ViewModel OK? Android 未找到数据绑定属性 - Android data binding attribute not found 数据绑定在另一个布局内膨胀布局 - Data Binding inflate layout inside another layout Android - 数据绑定找不到方法,使用 BottomSheetBehavior - Android - Data Binding cannot find method, using BottomSheetBehavior Android 数据绑定 - “未找到属性的资源标识符” - Android data binding - 'No resource identifier found for attribute' 片段中的Android数据绑定 - IllegalArgumentException:找不到视图 - Android data binding in fragment - IllegalArgumentException: No view found
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM