问题描述
我正在尝试将数据绑定与自定义视图结合使用(George Mount 在此处展示了一种可能的用法).
I'm trying to use databinding with custom views (a possible usage George Mount showed here).
如果没有 <merge>
标签,人们无法想象构建复合视图.但是,在这种情况下数据绑定失败:
One can't imagine building compound views without <merge>
tag. However, in this situation databinding fails:
MyCompoundView
类:
public class MyCompoundView extends RelativeLayout {
MyCompoundViewBinding binding;
public MyCompoundView (Context context, AttributeSet attrs) {
super(context, attrs);
init(context);
}
private void init(Context context){
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
binding = MyCompoundViewBinding.inflate(inflater, this, true);
}
my_compound_view.xml
:通过 app:isGone="@{!data.isViewVisible}"
我希望控制整个复合视图的可见性
my_compound_view.xml
: by app:isGone="@{!data.isViewVisible}"
i hoped to control the visibility of the whole compound view
<?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="data" type="com.example.MyViewModel"/>
</data>
<merge
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:isGone="@{!data.isViewVisible}">
<ImageView
android:id="@+id/image_image"
android:layout_width="60dp"
android:layout_height="60dp"
app:imageUrl="@{data.imagePhotoUrl}"/>
<!-- tons of other views-->
</merge>
</layout>
编译器错误:
Error:(13) No resource identifier found for attribute 'isGone' in package 'com.example'
Error:(17, 21) No resource type specified (at 'isGone' with value '@{!data.isViewVisible}').
我拥有所有需要的 @BindingAdapter
方法.现在我从 FrameLayout
继承视图并使用 而不是
- 它可以工作.但我有额外的嵌套布局.
I have all needed @BindingAdapter
methods. Now I inherit the view from FrameLayout
and use <RelativeLayout>
instead of <merge>
- and it works. But I have extra nested layout.
问题: merge
属性被忽略.有什么办法可以解决这个问题吗?
Question: merge
attrs are ignored. Is there any way to workaround that?
Android Studio 1.5.1 稳定版
Android Studio 1.5.1 stable
Gradle 插件 com.android.tools.build:gradle:1.5.0
推荐答案
膨胀后没有合并对象,所以没有什么可以用合并标签赋值的.我想不出任何可以用于合并的绑定标签.
There is no merge object after inflation, so there is nothing to assign values to with a merge tag. I can't think of any binding tag that will work on merge.
您可以将标记分配给根元素并使用 BindingAdapter 来执行您想要的操作.
You can assign the tag to the root element and use the BindingAdapter to do what you want.
<layout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
>
<data>
<variable name="data" type="com.example.MyViewModel"/>
</data>
<merge
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageView
app:isGone="@{!data.isViewVisible}"
android:id="@+id/image_image"
android:layout_width="60dp"
android:layout_height="60dp"
app:imageUrl="@{data.imagePhotoUrl}"/>
<!-- tons of other views-->
</merge>
</layout>
如果你想对 Binding 类本身做一些事情,你可以使用 DataBindingUtil 从 View 中找到对象.
If you want to do something with the Binding class itself, you can use the DataBindingUtil to find the object from the View.
@BindingAdapter("isGone")
public static void setGone(View view, boolean isGone) {
ViewDataBinding binding = DataBindingUtil.findBinding(view);
//... do what you want with the binding.
}
这篇关于Android 数据绑定不适用于 <merge>属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!