2016-02-23 6 views
13

私はカスタムビューでデータバインディングを使用しようとしています(George Mountが示した可能性のある使用here)。Androidのデータバインディングが<merge>属性で動作していません

<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:私は、化合物全体のビューの表示を制御

<?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> 

コンパイルエラーすることを望んだapp:isGone="@{!data.isViewVisible}"によって:

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}'). 

私はすべてが必要です方法。今私はFrameLayoutからの見解を継承し、<merge>の代わりに<RelativeLayout>を使用して動作します。しかし、私は追加のネストされたレイアウトを持っています。

質問:merge attrsは無視されます。回避策はありますか?

のAndroid Studioの1.5.1安定

のGradleプラグインcom.android.tools.build:gradle:1.5.0

+0

の代わりにを使用するのが最善です。 –

+2

@Rakeeb Rajbhandari、はい、データバインディングは ''タグでうまくいきます。ただし、カスタムビューを拡張しているときに、これにより余分なビューグループの問題は解決されません。 –

+0

私の質問を見てください。私はincludeタグと内部クラスを使用しています。 https://stackoverflow.com/q/45143721/5214893 –

答えて

8

インフレの後には、マージ対象はありませんので、マージタグ付きに値を割り当てることは何もありません。私はマージで動作するバインディング・タグを考えることはできません。

タグをルート要素に割り当て、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を使用することができます。 Androidのドキュメントから

@BindingAdapter("isGone") 
public static void setGone(View view, boolean isGone) { 
    ViewDataBinding binding = DataBindingUtil.findBinding(view); 
    //... do what you want with the binding. 
} 
+2

Android開発者は[documentation](https://developer.android.com/topic/libraries/data-binding/index)でそれについて言及しています。html#layout_details) – MrOnyszko

+0

あなたのアドバイスをいくつかのコードで詳しく説明できますか?ありがとうございました:) –

+2

こんにちは、私はマージタグを動作させることができません。 'Element merge is not allowed here 'というエラーが表示されます。参照:https://pastebin.com/QhQKYN71 –

0

Data Binding Library: Includes

変数が含まれるレイアウトの属性でアプリケーションの名前空間と変数 名を使用してレイアウトを含む からのバインディングに渡すことも

<?xml version="1.0" encoding="utf-8"?> 
<layout xmlns:android="http://schemas.android.com/apk/res/android" 
     xmlns:bind="http://schemas.android.com/apk/res-auto"> 
    <data> 
     <variable name="user" type="com.example.User"/> 
    </data> 
    <LinearLayout 
     android:orientation="vertical" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent"> 
     <include layout="@layout/name" 
      bind:user="@{user}"/> 
     <include layout="@layout/contact" 
      bind:user="@{user}"/> 
    </LinearLayout> 
</layout> 

データバインディングオブジェクトを<include>タグ(この例では:bind:[email protected]{user})を追加し、先頭にxmlns:bind="http://schemas.android.com/apk/res-auto"を追加します。