找不到用于我的绑定的充气方法(使用Android,数据绑定)
我正在使用数据绑定来绑定Android应用程序中的布局 我已经设置了布局(my_custom.xml)并生成了绑定类(MyCustomBinding),但Android Studio似乎没有立即找到绑定类的.inflate(…)方法,并将其标记为错误(红色文本!) 尽管如此,代码似乎是正确的,因为它编译并构建成一个APK 如何让Android Studio正确更新 代码示例: 这是我的自定义视图代码:找不到用于我的绑定的充气方法(使用Android,数据绑定),android,android-layout,android-databinding,Android,Android Layout,Android Databinding,我正在使用数据绑定来绑定Android应用程序中的布局 我已经设置了布局(my_custom.xml)并生成了绑定类(MyCustomBinding),但Android Studio似乎没有立即找到绑定类的.inflate(…)方法,并将其标记为错误(红色文本!) 尽管如此,代码似乎是正确的,因为它编译并构建成一个APK 如何让Android Studio正确更新 代码示例: 这是我的自定义视图代码: public class MyCustomView extends FrameLayo
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>
<data class="MyCustomBinding">
</data>
</layout>
从:
有时绑定不能预先知道。在这种情况下,可以使用DataBindingUtil类创建绑定:
在Android Studio中有些东西没有完成,因为您还没有实际实现数据绑定。 将变量添加到布局的
数据
元素后,将按预期找到充气
方法。也就是说,通过直接通过绑定设置文本字段的值,实际上并没有获得数据绑定的好处。您应该在绑定中设置视图模型,然后让绑定相应地更新视图。例如:
创建视图模型:
public class MyViewModel {
public final ObservableField<String> name;
public MyViewModel(String name) {
this.name = new ObservableField<>(name);
}
}
当然,最好还是通过自定义视图类中的setter传递数据,或者甚至从容器视图传递数据(请参见)您不需要那种类型的东西,数据绑定包括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");
}
这是基于最新数据绑定和androidx的更新答案,我来到这里是为了解决我自己的问题,在看了上面的答案后,我用自己的工作代码片段开发了这些答案。希望这有帮助
public class CustomActionBar1 extends RelativeLayout {
public MutableLiveData<String> title = new MutableLiveData<>("Sample text");
CustomActionBar1Binding binding;
public CustomActionBar1(Context context) {
super(context);
init(context, this);
}
public CustomActionBar1(Context context, AttributeSet attrs) {
super(context, attrs);
init(context, this);
}
public CustomActionBar1(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init(context, this);
}
public CustomActionBar1(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
init(context, this);
}
private void init(Context context, ViewGroup viewGroup) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
binding = DataBindingUtil.inflate(inflater, R.layout.custom_action_bar_1, viewGroup, true);
}
// helper to change title
public void changeTitle(String title) {
if (title != null)
this.title.setValue(title);
}
public void setTitleVisibility(Boolean visibile){
binding.titleText.setVisibility(visibile ? View.VISIBLE : View.INVISIBLE);
}
}
公共类CustomActionBar1扩展了RelativeLayout{
public MutableLiveData title=新的MutableLiveData(“示例文本”);
CustomActionBar1绑定绑定;
公共CustomActionBar1(上下文){
超级(上下文);
init(上下文,this);
}
公共CustomActionBar1(上下文、属性集属性){
超级(上下文,attrs);
init(上下文,this);
}
公共CustomActionBar1(上下文上下文、属性集属性、int defStyleAttr){
super(上下文、attrs、defStyleAttr);
init(上下文,this);
}
公共CustomActionBar1(上下文上下文、属性集属性、int-defStyleAttr、int-defStyleRes){
super(context、attrs、defStyleAttr、defStyleRes);
init(上下文,this);
}
私有void init(上下文上下文,视图组视图组){
LayoutFlater充气器=(LayoutFlater)context.getSystemService(context.LAYOUT\u充气器\u服务);
绑定=数据绑定直到充气(充气器,R.layout.custom\u action\u bar\u 1,viewGroup,true);
}
//帮助者更改标题
公共无效更改标题(字符串标题){
如果(标题!=null)
此.title.setValue(title);
}
public void setTitlevibility(布尔可见){
binding.titleText.setVisibility(可视?View.VISIBLE:View.INVISIBLE);
}
}
Xml
请发布您的代码。你使用的是活动还是片段?发布“MyCutomBinding”类代码。在我看来,这是Android Studio 2.2中的一个bug。在今天升级之前,我已经使用了数月没有这个问题的数据绑定,现在我也看到了它。如果你想看的话,有一个答案。谢谢你的回答。但我不相信这能以最好的方式回答我的问题。我认为这不是MyCustomBinding未知的情况之一。我在androidstudio中导入了这个类,并编译了它。奇怪的是,这个.inflate()方法没有被识别出来。它是编译的吗?哦我的错-但是你试过在xml中设置类吗?是的,它可以编译。我相信这主要是安卓工作室的一个问题。我尝试过在xml中设置类。虽然这允许我更改绑定的类名,但不幸的是它并没有解决这个问题。谢谢!是的,我意识到使用数据绑定的预期方式是使用模式,然后绑定xml中的值。我发现在代码中进行绑定比创建模型对象等更容易。我现在的解决方案是定义一个空模型类:public class EmptyModel{},然后将其定义为变量[],而不实际绑定到任何视图。
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>
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);
}
}
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");
}
public class CustomActionBar1 extends RelativeLayout {
public MutableLiveData<String> title = new MutableLiveData<>("Sample text");
CustomActionBar1Binding binding;
public CustomActionBar1(Context context) {
super(context);
init(context, this);
}
public CustomActionBar1(Context context, AttributeSet attrs) {
super(context, attrs);
init(context, this);
}
public CustomActionBar1(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init(context, this);
}
public CustomActionBar1(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
init(context, this);
}
private void init(Context context, ViewGroup viewGroup) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
binding = DataBindingUtil.inflate(inflater, R.layout.custom_action_bar_1, viewGroup, true);
}
// helper to change title
public void changeTitle(String title) {
if (title != null)
this.title.setValue(title);
}
public void setTitleVisibility(Boolean visibile){
binding.titleText.setVisibility(visibile ? View.VISIBLE : View.INVISIBLE);
}
}
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<data>
<variable
name="customActionBar1"
type="com.actionBar.CustomActionBar1" />
</data>
<RelativeLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:background="@color/yellow"
android:layout_height="@dimen/action_bar_height"
android:id="@+id/main_header_relative">
<TextView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:text="@{customActionBar1.title}"
android:layout_centerInParent="true"
android:gravity="center"
android:id="@+id/titleText"
android:visibility="visible"
android:textColor="@color/black" />
</RelativeLayout>
</layout>