2017-04-13 17 views
2

私はデータバインディングに関する問題に悩まされています。特に、カスタムバインディングに@BindingAdapterを使用しています。Androidデータバインディング属性app:itemIconTintのアプリケーションネームスペースは無視されます

私はBottomNavigationViewを持っており、ブール値に応じて特定のColorStateListをロードするためにitemIconTintitemTextColorをバインドしたいと思います。

だから私は、次の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" 
    xmlns:tools="http://schemas.android.com/tools" 
    tools:context="mypackage.HomeActivity"> 

    <data> 
     <variable 
      name="utils" 
      type="mypackage.model.Utils" /> 
    </data> 

    <android.support.constraint.ConstraintLayout 
     android:id="@+id/container" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:orientation="vertical"> 

     <FrameLayout 
      android:id="@+id/content" 
      android:layout_width="0dp" 
      android:layout_height="0dp" 
      app:layout_constraintBottom_toTopOf="@+id/bottomNavigationView" 
      app:layout_constraintLeft_toLeftOf="parent" 
      app:layout_constraintRight_toRightOf="parent" 
      app:layout_constraintTop_toTopOf="parent" 
      tools:layout_constraintLeft_creator="1" 
      tools:layout_constraintRight_creator="1" 
      tools:layout_constraintTop_creator="1"> 

     </FrameLayout> 

     <android.support.design.widget.BottomNavigationView 
      android:id="@+id/bottomNavigationView" 
      android:layout_width="0dp" 
      android:layout_height="wrap_content" 
      android:background="@{utils.lawyer ? @color/greyish_brown : @color/white}" 
      app:itemIconTint="@{utils.lawyer ? @colorStateList/nav_item_color_state_lawyer : @colorStateList/nav_item_color_state_user}" 
      app:itemTextColor="@{utils.lawyer ? @colorStateList/nav_item_color_state_lawyer : @colorStateList/nav_item_color_state_user}" 
      app:layout_constraintBottom_toBottomOf="parent" 
      app:layout_constraintLeft_toLeftOf="parent" 
      app:layout_constraintRight_toRightOf="parent" 
      app:menu="@menu/navigation" 
      tools:layout_constraintBottom_creator="1" 
      tools:layout_constraintLeft_creator="1" 
      tools:layout_constraintRight_creator="1" /> 

    </android.support.constraint.ConstraintLayout> 

</layout> 

そしてここでは、クラスUtilsのである:

public class Utils { 

    private boolean lawyer; 

    public boolean isLawyer() { 
     return lawyer; 
    } 

    public void setLawyer(boolean lawyer) { 
     this.lawyer = lawyer; 
    } 


    @BindingAdapter({"app:itemIconTint"}) 
    public static void setItemIconTint(BottomNavigationView view, ColorStateList colorStateList) { 

     view.setItemIconTintList(colorStateList); 
    } 


    @BindingAdapter({"app:itemTextColor"}) 
    public static void setItemTextColor(BottomNavigationView view, ColorStateList colorStateList) { 

     view.setItemTextColor(colorStateList); 
    } 
} 

だから私の目標は、ブールlawyerの応じて、異なるColorStateListをロードすることです。これにより、BottomNavigationViewはダイナミックなデザインになります。

私はコンパイルするとき、私はこのバグを持っている:あなたの助けのための

14:13:42.540 [ERROR] [system.err] Note: processing adapters 
14:13:42.540 [ERROR] [system.err] Note: building generational class cache 
14:13:42.540 [ERROR] [system.err] Note: loaded item android[email protected]49d08673 from file 
14:13:42.540 [ERROR] [system.err] Note: loaded item android.da[email protected]4e48888f from file 
14:13:42.540 [ERROR] [system.err] Note: loaded item [email protected]5f from file 
14:13:42.540 [ERROR] [system.err] /Users/.../model/Utils.java:27: warning: Application namespace for attribute app:itemIconTint will be ignored. 
14:13:42.540 [ERROR] [system.err]  public static void setItemIconTint(BottomNavigationView view, ColorStateList colorStateList) { 
14:13:42.540 [ERROR] [system.err]      ^
14:13:42.540 [ERROR] [system.err] Note: STORE addBindingAdapter itemIconTint setItemIconTint(android.support.design.widget.BottomNavigationView,int) 
14:13:42.540 [ERROR] [system.err] Note: BINARY created method desc 2 mypackage.model.Utils setItemIconTint, setItemIconTint(android.support.design.widget.BottomNavigationView,android.content.res.ColorStateList) 
14:13:42.540 [ERROR] [system.err] /Users/.../model/Utils.java:34: warning: Application namespace for attribute app:itemTextColor will be ignored. 
14:13:42.540 [ERROR] [system.err]  public static void setItemTextColor(BottomNavigationView view, ColorStateList colorStateList) { 
14:13:42.540 [ERROR] [system.err]      ^
14:13:42.540 [ERROR] [system.err] Note: STORE addBindingAdapter itemTextColor setItemTextColor(android.support.design.widget.BottomNavigationView,android.content.res.ColorStateList) 
14:13:42.540 [ERROR] [system.err] Note: BINARY created method desc 2 mypackage.Utils setItemTextColor, setItemTextColor(android.support.design.widget.BottomNavigationView,android.content.res.ColorStateList) 

感謝を!

編集:2貴様の色のファイルはフォルダから来る色 enter image description here

編集2:@tynnは、リソースタイプについて知らないAndroidのデータバインディング、言ったように。ですから、Color ressource @colorは `@colorStateList 'に置き換えなければなりません。だから私は変更を行い、私のカスタムBindingAdapterもintの代わりにColorStateListを受け取るように更新されました。問題は同じです

解決済み:カスタムバインディングアダプターの必要はありません。データバインディングは、各クラスの既存のセッターに基づいて自動生成するためです(Attribute Settersをチェックしてください)。私の問題は奇妙で、Rファイルのために、ひどく生成されました。

答えて

1

色と色の状態リストを混在させています。前者はちょうどintで、後者はColorStateListでなければなりません。データバインディングの最後にExpression Languageのマニュアルがあると、@colorの代わりに@colorStateListを使用する必要があることがわかります。また、@colorは、カラーリソースではなく、カラーintになります。

あなたのアダプタでは、名前空間なしで属性名を使用する必要があります。

最終的には、アダプタを定義せずにapp:itemTextColorapp:itemIconTintListに設定するだけで済みます。プロセッサは正しいセッターsetItemTextColor(ColorStateList)setItemIconTintList(ColorStateList)を選択します。

<android.support.design.widget.BottomNavigationView 
    android:id="@+id/bottomNavigationView" 
    android:layout_width="0dp" 
    android:layout_height="wrap_content" 
    android:background="@{utils.lawyer ? @color/greyish_brown : @color/white}" 
    app:itemIconTintList="@{utils.lawyer ? @colorStateList/nav_item_color_state_lawyer : @colorStateList/nav_item_color_state_user}" 
    app:itemTextColor="@{utils.lawyer ? @colorStateList/nav_item_color_state_lawyer : @colorStateList/nav_item_color_state_user}" 
    app:menu="@menu/navigation" /> 
+0

返信いただきありがとうございます。カスタムBindingAdapterの更新方法を確認します。また、いくつかの属性にはデフォルトセッターはありません。これは 'itemIconTint'と' itemTextColor'の場合です。だから、私は自分の必要性に合わせて作成する必要があります – Guimareshh

+0

はい、私は使用しています:android.support.design.widget.BottomNavigationView – Guimareshh

+0

私はそれをやったし、それは動作していない(クリーニング/ビルド後も)。また、 'itemIconTintList'は別の問題を引き起こしています。本当に良い属性は 'itemIconTint'だと思います。しかし、私のカスタムBindingAdapterを削除しても、私はあなたに言った問題があります(セッターの不足について): 'Error :(38、33) 'app:itemIconTint'のセッターが見つかりませんでした。 .ColorStateListはandroid.support.design.widget.BottomNavigationViewにあります。' – Guimareshh

関連する問題