Android 为什么忽略此自定义视图属性?

Android 为什么忽略此自定义视图属性?,android,android-layout,android-databinding,android-attributes,Android,Android Layout,Android Databinding,Android Attributes,我正在尝试使用为任何视图定义属性,如中所述 要做到这一点,文章说首先需要一个带有标签的布局: <?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"> <LinearLayout android:ori

我正在尝试使用为任何视图定义属性,如中所述

要做到这一点,文章说首先需要一个带有
标签的布局:

<?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">
    <LinearLayout android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <TextView android:id="@android:id/text1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:attribute='@{"name"}'/>
    </LinearLayout>
</layout>
(这篇文章没有提到它,但我必须在我的
build.gradle中启用
dataBinding
,这是在我可以构建之前在中建议的。)

文章接着解释了如何编写
BindingAdapter
方法来处理属性:

import android.databinding.BindingAdapter;
import android.util.Log;
import android.view.View;

public class AttributesBindingAdapter {
    @BindingAdapter("bind:attribute")
    public static void bindAttribute(View view, String attributeName){
        Log.e("PLN", attributeName);
    }
}
但是,从不调用
bindAttribute
方法。我确实在我的build文件夹中看到了为布局生成的代码,但是没有发生其他事情


为什么我的BindingAdapter被忽略?从第一个链接中可以看出,
data
标记存在。它可能在G+帖子中被省略,因为它是样板。事实上,在文件中说

数据绑定布局文件略有不同,首先是布局的根标记,然后是数据元素和视图根元素

无论如何,我想你可能在布局文件中遗漏了一些必需的糖。你能试试吗

app:attribute='@{"name"}`

可能需要进行绑定。我的意思是,现在我的目标是盲目的,直到我真正测试这个。但是从那篇文章中我看到了
app:imageUrl='@{”http://example.com/image.jpg“}”

从我在第一个链接中可以看出,
数据
标记存在。它可能在G+帖子中被省略,因为它是样板。事实上,在文件中说

数据绑定布局文件略有不同,首先是布局的根标记,然后是数据元素和视图根元素

无论如何,我想你可能在布局文件中遗漏了一些必需的糖。你能试试吗

app:attribute='@{"name"}`

可能需要进行绑定。我的意思是,现在我的目标是盲目的,直到我真正测试这个。但是从那篇文章中我看到了
app:imageUrl='@{”http://example.com/image.jpg“}'

它应该是
@BindingAdapter(“bind:attribute”)
而不是
@BindingAdapter(“app:attribute”)

试试这个,它可能会有用

app:attribute="@{`name`}"

它应该是
@BindingAdapter(“bind:attribute”)
,而不是
@BindingAdapter(“app:attribute”)

试试这个,它可能会有用

app:attribute="@{`name`}"

我找到了问题的解决方案,但没有正确创建绑定:

按照的第一步,我使用了
DataBindingUtil.setContentView
,但是对于
ListView
项目,您需要使用
ItemBinding.inflate
适配器的
ViewHolder

@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    ViewDataBinding binding = DataBindingUtil.inflate(
            LayoutInflater.from(parent.getContext()),
                    R.layout.item, parent, false);
    return new ViewHolder(binding.getRoot());
}

我找到了问题的解决方案,但没有正确创建绑定:

按照的第一步,我使用了
DataBindingUtil.setContentView
,但是对于
ListView
项目,您需要使用
ItemBinding.inflate
适配器的
ViewHolder

@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    ViewDataBinding binding = DataBindingUtil.inflate(
            LayoutInflater.from(parent.getContext()),
                    R.layout.item, parent, false);
    return new ViewHolder(binding.getRoot());
}

我同意你的观点,一个人应该按照指南使用这个数据标签,我就是这么做的。但问题依然存在。至于你链接的帖子,你认为它有什么帮助?OP有一个编译时错误(而我的应用程序生成并运行,但忽略了我的BindingAdapter),使用
@BindingMethods(@BindingMethod(…)
而不是
@BindingAdapter
的可接受解决方案仍然会导致无法调用该方法。你能说得更具体一点吗?@PLNech我添加的链接应该消失了。我误解了这个问题。添加
@{}
没有帮助吗?也许可以尝试添加它,就像它在帖子中一样。所以
app:attribute='@{“name”}'
。你的语法可能是对的(我更新了这个问题以反映它)。然而,即使使用
app:attribute='@{“name”}
,该方法仍然没有被调用…@PLNech您的布局文件名是什么?
item.xml
(它将用于在
列表视图中显示项目)
),我同意您的观点,应该遵循指南并使用这个数据标记,我确实这么做了。但问题依然存在。至于你链接的帖子,你认为它有什么帮助?OP有一个编译时错误(而我的应用程序生成并运行,但忽略了我的BindingAdapter),使用
@BindingMethods(@BindingMethod(…)
而不是
@BindingAdapter
的可接受解决方案仍然会导致无法调用该方法。你能说得更具体一点吗?@PLNech我添加的链接应该消失了。我误解了这个问题。添加
@{}
没有帮助吗?也许可以尝试添加它,就像它在帖子中一样。所以
app:attribute='@{“name”}'
。你的语法可能是对的(我更新了这个问题以反映它)。但是,即使使用
app:attribute='@{“name”}
,该方法仍然没有被调用…@PLNech您的布局文件名是什么?
item.xml
(它将用于在
列表视图中显示项目
),它应该是
@BindingAdapter(“bind:attribute”)
,而不是
@BindingAdapter(“app:attribute”)
,您应该参考它应该是
@BindingAdapter(“bind:attribute”)
,而不是
@BindingAdapter(“app:attribute”)
,您应该参考