Android数据绑定与条件资源

14

这是Android数据绑定的典型用法:

android:background="@{isError ? @color/red : @color/white}"

当状态可以采用多个值时,问题会变得更加复杂。编辑:在方法调用中使用status属性是使其正常工作的唯一方法:

android:background="@{Check.getStatusColor(check.status)}"

并定义静态方法(不需要@Bindable注解):

public int getStatusColor(int status) {
    switch (status.get()) {
        case STATUS_OK:
            return ContextCompat.getColor(context, R.color.success);
        case STATUS_WARNING:
            return ContextCompat.getColor(context, R.color.warning);
        case STATUS_ERROR:
            return ContextCompat.getColor(context, R.color.error);
        default:
            return ContextCompat.getColor(context, R.color.idle);
    }
}

如何实现这一点,而不必在XML中嵌套三元运算符(我认为这并不优雅),也不必传递 check.status 属性?

编辑:添加XML:

<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android">
    <data>
        <import type="org.honorato.diagnostics.models.Check"/>
        <variable
            name="check"
            type="Check"/>
    </data>
    <LinearLayout
        android:background="@android:color/white"
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:minHeight="72dp"
        >

        <LinearLayout
            android:padding="16dp"
            android:layout_width="0dip"
            android:layout_weight="1"
            android:layout_height="wrap_content"
            android:orientation="vertical">

            <TextView
                android:text="@{check.title}"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:textColor="@android:color/primary_text_light"
                android:textStyle="bold" />

        </LinearLayout>

        <ImageView
            android:padding="16dp"
            android:src="@{check.getStatusDrawable(check.status)}"
            android:background="@{check.getStatusColor(check.status)}"
            android:layout_width="72dp"
            android:layout_height="match_parent"
            android:layout_gravity="center_vertical|center_horizontal" />

    </LinearLayout>
</layout>

请添加您的 XML 文件数据。方法 getColorForSttus 是否具有 @Bindable 注释? - Natali
@Natali 更新了问题。我进行了一些修改以使其工作,但仍然觉得不够优雅。将尝试使用Bindable注释。 - jlhonora
尝试在 getStatusColor(int status) 方法中添加 @Bindable 注解。之后你可以在 xml 中使用它,例如 @{check.statusColor}。 - Natali
1个回答

5
我将以如下方式实现此目标:
android:background="@{check.getStatusColor()}"

getStatusColorCheck 类的一个非静态方法,所以我们可以访问实例字段 status

public int getStatusColor() {
    switch (status) {
        case STATUS_OK:
            return ContextCompat.getColor(context, R.color.success);
        case STATUS_WARNING:
            return ContextCompat.getColor(context, R.color.warning);
        case STATUS_ERROR:
            return ContextCompat.getColor(context, R.color.error);
        default:
            return ContextCompat.getColor(context, R.color.idle);
    }
}

这应该可以正常运行。

一个注意点:使用这种语法时,我认为当调用notifyPropertyChanged(BR.statusColor)时,背景不会更新。这只会更新使用"@{check.statusColor}"语法的表达式。 - Lorne Laliberte

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接