几个小时后我就遇到了数据绑定问题,特别是使用@BindingAdapter进行自定义绑定。
我有一个BottomNavigationView,我希望绑定itemIconTint
和itemTextColor
以根据布尔值加载特定的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);
}
}
所以我的目标是根据布尔值ColorStateList
加载不同的lawyer
。有了它,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.databinding.annotationprocessor.ProcessBindable$IntermediateV1@49d08673 from file
14:13:42.540 [ERROR] [system.err] Note: loaded item android.databinding.annotationprocessor.ProcessExpressions$IntermediateV2@4e48888f from file
14:13:42.540 [ERROR] [system.err] Note: loaded item android.databinding.tool.store.SetterStore$IntermediateV3@11787d5f 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:正如@tynn所说,Android数据绑定并不了解资源类型。因此,对于Color ressource @color
必须由`@colorStateList替换。所以我做了更改,我的自定义BindingAdapter也已更新,以接收ColorStateList而不是int。问题是一样的
问题已解决:无需自定义BindingAdapter,因为数据绑定会根据每个类中的现有setter自动生成setter(请查看Attribute Setters)。我的问题很奇怪,因为R文件很难生成。
答案 0 :(得分:1)
您正在混合颜色和颜色状态列表。前者仅为int
,而后者应为ColorStateList
。在数据绑定Expression Language文档的末尾,您会发现需要使用@colorStateList
而不是@color
。同样@color
会产生颜色int,而不是颜色资源。
对于适配器,您还必须使用不带名称空间的属性名称。
但最终在您的情况下,您只需设置app:itemTextColor
和app: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" />