【发布时间】:2016-06-06 01:28:29
【问题描述】:
我正在尝试将数据绑定与自定义视图一起使用(George Mount 展示了here 的可能用法)。
无法想象没有<merge> 标签的复合视图。但是,在这种情况下,数据绑定会失败:
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: by app:isGone="@{!data.isViewVisible}" 我希望控制整个复合视图的可见性
<?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> - 它可以工作。但我有额外的嵌套布局。
问题: merge attrs 被忽略。有没有办法解决这个问题?
Android Studio 1.5.1 稳定版
Gradle 插件com.android.tools.build:gradle:1.5.0
【问题讨论】:
-
最好使用
而不是 。 -
@Rakeeb Rajbhandari,是的,数据绑定在
<include>标签上工作正常。但是,这并不能解决膨胀自定义视图时额外视图组的问题。 -
请看我的问题。我正在使用包含标签和内部类。 stackoverflow.com/q/45143721/5214893
标签: android data-binding android-databinding