2017-04-13 34 views
2

由於幾個小時我在數據綁定的問題上停滯不前,特別是使用@BindingAdapter進行自定義綁定。Android數據綁定屬性app:itemIconTint的應用程序名稱空間將被忽略

我有一個BottomNavigationView,我想結合itemIconTintitemTextColor加載取決於一個布爾值的特定ColorStateList。

所以我有以下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) 

感謝您的幫助!

編輯:兩型動物彩色文件來自文件夾顏色 enter image description here

編輯2:如@tynn表示,Android數據綁定不知道資源類型。所以對於顏色資源@color必須用`@colorStateList替換。所以我做了更改,並且我的自定義BindingAdapter已經更新,以便接收ColorStateList而不是int。問題是一樣的

已解決問題:無需自定義BindingAdapter,因爲數據綁定會根據每個類中現有的setter自動生成setter(檢查Attribute Setters)。我的問題很奇怪,而且由於R文件的原因,它的產生很糟糕。

回答

1

你在混合顏色和顏色狀態列表。前者只是int而後者應該是ColorStateList。在數據綁定Expression Language文檔的末尾,您發現需要使用@colorStateList而不是@color。另外@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我有我告訴你的問題(關於丟失的setter): '錯誤:(38,33)找不到屬性'app:itemIconTint'的參數類型爲android.content.res的setter 。android.support.design.widget.BottomNavigationView上的ColorStateList。' – Guimareshh