[英]Android data binding is not working with <merge> attributes
我正在嘗試將數據綁定與自定義視圖一起使用(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);
}
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
膨脹后沒有合並對象,所以沒有什么可以用合並標簽賦值的。 我想不出任何可以用於合並的綁定標簽。
您可以將標記分配給根元素並使用 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 類本身做一些事情,你可以使用 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{
//...
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.