简体   繁体   中英

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.

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!).

The code seems to be correct though, since it compiles and builds just fine into an APK.

How do I get Android Studio to update correctly ?

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. 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:

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)

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 )

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:

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:

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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