0

この Android Developers postで説明されているように、 Data Binding Libraryを使用して任意のビューの属性を定義しようとしています。

<layout>そのためには、最初にタグを囲むレイアウトが必要であると投稿は述べています。

<?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">
    <LinearLayout android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <TextView android:id="@android:id/text1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:attribute='@{"name"}'/>
    </LinearLayout>
</layout>

この時点で、レイアウトはClassNotFoundExceptionwhen inflatedを引き起こしました。それを取り除く唯一の方法は、<data></data>ノードを追加することでした。たとえ Android 開発者の投稿にはなかったとしてもです。

<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
    <data></data>
    ...
</layout>

(投稿では言及されていませんが、ビルドする前に、ガイドで推奨されdataBindingているように有効にする必要がありました。)build.gradle

BindingAdapterこの投稿では、属性を処理するメソッドの書き方について説明しています。

import android.databinding.BindingAdapter;
import android.util.Log;
import android.view.View;

public class AttributesBindingAdapter {
    @BindingAdapter("bind:attribute")
    public static void bindAttribute(View view, String attributeName){
        Log.e("PLN", attributeName);
    }
}

ただし、bindAttributeメソッドが呼び出されることはありません。ビルド フォルダーにレイアウト用に生成されたコードが表示されますが、他に何も起こりません。

BindingAdapter が無視されるのはなぜですか?

4

3 に答える 3