问题描述
我正在尝试使用数据绑定库,如此Android开发人员帖子中所述.. >
为此,该帖子说,首先需要一种带有<layout>
标记的布局:
<?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>
这时,导致布局的原因ClassNotFoundException
膨胀时.我发现摆脱它的唯一方法是添加<data></data>
节点,即使Android Developers帖子中不存在该节点:
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<data></data>
...
</layout>
(该帖子未提及它,但是我必须按照指南,然后再进行构建.)
然后,该帖子解释了如何编写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
方法.我的构建文件夹中确实看到了布局的生成代码,但没有其他反应.
为什么我的BindingAdapter被忽略?
我找到了解决问题的方法,但我没有正确创建绑定:
按照指南的第一步,我使用了,但对于ListView
项目,您需要在Adapter
的ViewHolder
中使用ItemBinding.inflate
:
@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());
}
I am trying to define an attribute for any view using the Data Binding Library, as explained in this Android Developers post.
To do so, the post says one first needs a layout with an enclosing <layout>
tag:
<?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>
At this point, the layout caused a ClassNotFoundException
when inflated. The only way I found to get rid of it was to add a <data></data>
node, even if it was absent from the Android Developers post:
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<data></data>
...
</layout>
(The post does not mention it, but I had to enable dataBinding
in my build.gradle
as recommended in the Guide before I could build.)
The post then explains how to write a BindingAdapter
method to process the attribute:
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);
}
}
However, the bindAttribute
method is never called. I do see the generated code for the layout in my build folder, but nothing else happens.
Why is my BindingAdapter ignored?
I found the solution to my problem, I was not creating the Binding correctly:
Following the first steps of the Guide, I used DataBindingUtil.setContentView
, but for ListView
items you need to use ItemBinding.inflate
in the Adapter
's 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());
}
这篇关于为什么忽略此自定义视图属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!