问题描述
我正在尝试使用数据绑定与自定义视图(可能的用法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
class:
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
继承视图,并使用< RelativeLayout>
而不是< merge> ;
- 它可以工作。但我有额外的嵌套布局。
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
attrs被忽略。有没有办法解决这个问题?
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来执行所需的操作。如果要使用Binding类本身做某事,可以使用DataBindingUtil从View中查找对象。
You can assign the tag to the root element and use the BindingAdapter to do what you want. If you want to do something with the Binding class itself, you can use the DataBindingUtil to find the object from the View.
这篇关于Android数据绑定无法使用< merge>属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!