Dmi*_*zin 18 data-binding android android-databinding
我正在尝试使用自定义视图的数据绑定(George Mount 在此处显示的可能用法).
无法想象在没有<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);
}
Run Code Online (Sandbox Code Playgroud)
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>
Run Code Online (Sandbox Code Playgroud)
编译器错误:
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}').
Run Code Online (Sandbox Code Playgroud)
我有所有需要的@BindingAdapter方法.现在我继承了视图FrameLayout并使用<RelativeLayout>而不是<merge>- 并且它有效.但我有额外的嵌套布局.
问题: merge attrs被忽略了.有没有办法解决这个问题?
Android Studio 1.5.1稳定
Gradle插件 com.android.tools.build:gradle:1.5.0
Geo*_*unt 13
在通货膨胀之后没有合并对象,因此没有任何内容可以使用合并标记来赋值.我想不出任何可用于合并的绑定标记.
您可以将标记分配给根元素,并使用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>
Run Code Online (Sandbox Code Playgroud)
如果要对Binding类本身执行某些操作,可以使用DataBindingUtil从View中查找对象.
@BindingAdapter("isGone")
public static void setGone(View view, boolean isGone) {
ViewDataBinding binding = DataBindingUtil.findBinding(view);
//... do what you want with the binding.
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
7732 次 |
| 最近记录: |