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

tre*_*ere 20 android android-layout android-databinding

我正在使用数据绑定来绑定我的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");
    }
}
Run Code Online (Sandbox Code Playgroud)

布局定义为:

    <?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>
Run Code Online (Sandbox Code Playgroud)

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

在此输入图像描述

Jer*_*all 9

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

创建一个视图模型:

public class MyViewModel {
    public final ObservableField<String> name;
    public MyViewModel(String name) {
        this.name = new ObservableField<>(name);
    }
}
Run Code Online (Sandbox Code Playgroud)

并在您的布局中使用它

<?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>
Run Code Online (Sandbox Code Playgroud)

(注意元素中variable声明的data内容,以及它在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);
    }
}
Run Code Online (Sandbox Code Playgroud)

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