2017-04-13 4 views
2

몇 시간 째로 저는 데이터 바인딩에 문제가 있습니다. 특히 맞춤 바인딩에 @BindingAdapter를 사용하고 있습니다.안드로이드 데이터 바인딩 속성 app : itemIconTint에 대한 응용 프로그램 네임 스페이스가 무시됩니다.

나는 BottomNavigationView가 있고 부울에 따라 특정 ColorStateList를로드하려면 itemIconTintitemTextColor을 바인딩하고 싶습니다. 여기

<?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를로드하는 것입니다

그래서 나는 다음과 같은 XML이있다. 이를 통해 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) 

감사합니다!

편집 : 두 개의 differents의 색상 파일 폴더 컬러 enter image description here

편집 2에서 오는 : @tynn 말했듯이, 바인딩 안드로이드 데이터 자원 유형에 대해 알고하지 않습니다. 그래서 Color ressource @color은`@colorStateList '로 대체되어야한다. 그래서 저는 변경 작업을 수행했으며, 사용자 정의 BindingAdapter는 int 대신 ColorStateList를 수신하도록 업데이트되었습니다. 문제가 동일합니다

문제 해결 : 데이터 바인딩이 각 클래스의 기존 설정자를 기반으로 설정 도구를 자동으로 생성하므로 (사용자 지정 BindingAdapter 필요 없음) 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를 삭제하더라도 나는 setter 누락 문제에 대해 이야기했다 : 'Error : (38, 33) 매개 변수 유형이 android.content.res 인 'app : itemIconTint'속성에 대한 setter를 찾을 수 없다. .ColorStateList on android.support.design.widget.BottomNavigationView.' – Guimareshh