Android 数据绑定不适用于 <merge> 属性
Android data binding is not working with <merge> attributes
我正在尝试将数据绑定与自定义视图结合使用(George Mount 展示了一种可能的用法 )。
没有 <merge>
标签就无法构建复合视图。但是,在这种情况下数据绑定失败:
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}"
我希望控制整个复合视图的可见性
<?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
属性被忽略。有什么办法可以解决这个问题吗?
Android Studio 1.5.1 稳定版
Gradle 插件 com.android.tools.build:gradle:1.5.0
inflation 之后没有合并对象,因此没有可使用合并标签赋值的对象。我想不出任何适用于合并的绑定标签。
您可以将标签分配给根元素并使用 BindingAdapter 执行您想要的操作。
<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 class 本身做一些事情,您可以使用 DataBindingUtil 从 View 中查找对象。
@BindingAdapter("isGone")
public static void setGone(View view, boolean isGone) {
ViewDataBinding binding = DataBindingUtil.findBinding(view);
//... do what you want with the binding.
}
实际上你可以在 <include>
中使用 <merge>
标签并进行数据绑定。
例如:
incl_button.xml
<layout>
<merge xmlns:android="http://schemas.android.com/apk/res/android">
<Button android:id="btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Click"
/>
</merge>
</layout>
fragment_example.xml
<layout xmlns:android="http://schemas.android.com/apk/res/android">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<include
layout="@layout/incl_button"
android:id="@+id/layout_btn" />
</LinearLayout>
</layout>
ExampleFragment.kt
binding.layoutBtn.btn.setOnClickListener{
//...
}
我正在尝试将数据绑定与自定义视图结合使用(George Mount 展示了一种可能的用法
没有 <merge>
标签就无法构建复合视图。但是,在这种情况下数据绑定失败:
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}"
我希望控制整个复合视图的可见性
<?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
属性被忽略。有什么办法可以解决这个问题吗?
Android Studio 1.5.1 稳定版
Gradle 插件 com.android.tools.build:gradle:1.5.0
inflation 之后没有合并对象,因此没有可使用合并标签赋值的对象。我想不出任何适用于合并的绑定标签。
您可以将标签分配给根元素并使用 BindingAdapter 执行您想要的操作。
<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 class 本身做一些事情,您可以使用 DataBindingUtil 从 View 中查找对象。
@BindingAdapter("isGone")
public static void setGone(View view, boolean isGone) {
ViewDataBinding binding = DataBindingUtil.findBinding(view);
//... do what you want with the binding.
}
实际上你可以在 <include>
中使用 <merge>
标签并进行数据绑定。
例如:
incl_button.xml
<layout>
<merge xmlns:android="http://schemas.android.com/apk/res/android">
<Button android:id="btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Click"
/>
</merge>
</layout>
fragment_example.xml
<layout xmlns:android="http://schemas.android.com/apk/res/android">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<include
layout="@layout/incl_button"
android:id="@+id/layout_btn" />
</LinearLayout>
</layout>
ExampleFragment.kt
binding.layoutBtn.btn.setOnClickListener{
//...
}