Android:CardView前台使用数据绑定

Android:CardView前台使用数据绑定,android,data-binding,android-cardview,Android,Data Binding,Android Cardview,我试图用CardView来填充我的RecyclerView,CardView使用Android数据绑定来设置文本视图中的文本等属性。对于未完成的项目,我想添加灰色覆盖。但不幸的是,前台android:foreground=“@{viewModel.getState!=State.FINISHED?@color/gray\u transparent54:null}”>未应用。另一方面,如果我强制使用前景色android:foreground=“@grey\u transparent54”>,它就会

我试图用CardView来填充我的RecyclerView,CardView使用Android数据绑定来设置文本视图中的文本等属性。对于未完成的项目,我想添加灰色覆盖。但不幸的是,前台
android:foreground=“@{viewModel.getState!=State.FINISHED?@color/gray\u transparent54:null}”>
未应用。另一方面,如果我强制使用前景色
android:foreground=“@grey\u transparent54”>
,它就会正常工作

<layout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools">
    <data class="ListItemBinder">
        <import type="myProject.State"/>
        <import type="android.view.View"/>
        <variable
            name="viewModel"
            type="myProject.ListItemViewModel"/>
    </data>

    <android.support.v7.widget.CardView
        xmlns:card_view="http://schemas.android.com/apk/res-auto"
        android:layout_width="match_parent"
        android:layout_height="80dp"
        card_view:cardCornerRadius="4dp"
        android:foreground="@{viewModel.getState != State.FINISHED? @color/gray_transparent54 : null}">
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@{viewModel.getName}"
                tools:text="Name"
                android:textColor="@color/color_text_primary"
                android:textSize="24sp"/>
    </android.support.v7.widget.CardView>
</layout>
<android.support.v7.widget.CardView
    xmlns:card_view="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="80dp"
    card_view:cardCornerRadius="4dp"
    android:foreground="@{viewModel.state != State.FINISHED? @color/gray_transparent54 : null}">
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@{viewModel.name}"
            tools:text="Name"
            android:textColor="@color/color_text_primary"
            android:textSize="24sp"/>
</android.support.v7.widget.CardView>


提前感谢。

除非您实现了诸如
getstate()
getname()
之类的getter,否则我认为您访问的
viewModel
字段不正确。虽然您没有发布您的
ListItemViewModel
,但我怀疑您需要更接近这一点的东西:

<android.support.v7.widget.CardView
    xmlns:card_view="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="80dp"
    card_view:cardCornerRadius="4dp"
    android:foreground="@{viewModel.state != State.FINISHED? @color/gray_transparent54 : null}">
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@{viewModel.name}"
            tools:text="Name"
            android:textColor="@color/color_text_primary"
            android:textSize="24sp"/>
</android.support.v7.widget.CardView>

<android.support.v7.widget.CardView
    xmlns:card_view="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="80dp"
    card_view:cardCornerRadius="4dp"
    android:foreground="@{viewModel.state != State.FINISHED? @color/gray_transparent54 : null}">
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@{viewModel.name}"
            tools:text="Name"
            android:textColor="@color/color_text_primary"
            android:textSize="24sp"/>
</android.support.v7.widget.CardView>

<android.support.v7.widget.CardView
    xmlns:card_view="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="80dp"
    card_view:cardCornerRadius="4dp"
    android:foreground="@{viewModel.state != State.FINISHED? @color/gray_transparent54 : null}">
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@{viewModel.name}"
            tools:text="Name"
            android:textColor="@color/color_text_primary"
            android:textSize="24sp"/>
</android.support.v7.widget.CardView>
当您访问
@{viewModel.name}
时,数据绑定会自动在
ListItemViewModel
类中查找名为“getName()”的方法;当您访问
@{viewModel.state}
等时,数据绑定会自动查找名为“getState()”的方法

<android.support.v7.widget.CardView
    xmlns:card_view="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="80dp"
    card_view:cardCornerRadius="4dp"
    android:foreground="@{viewModel.state != State.FINISHED? @color/gray_transparent54 : null}">
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@{viewModel.name}"
            tools:text="Name"
            android:textColor="@color/color_text_primary"
            android:textSize="24sp"/>
</android.support.v7.widget.CardView>
注意:尽管UI逻辑通常是可以的,但要小心将业务逻辑放在XML中,因为它会限制您的灵活性,并使测试/调试更加困难

<android.support.v7.widget.CardView
    xmlns:card_view="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="80dp"
    card_view:cardCornerRadius="4dp"
    android:foreground="@{viewModel.state != State.FINISHED? @color/gray_transparent54 : null}">
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@{viewModel.name}"
            tools:text="Name"
            android:textColor="@color/color_text_primary"
            android:textSize="24sp"/>
</android.support.v7.widget.CardView>

还要知道,在使用三元(
?:
)运算符时不能混合类型,因此在颜色int和可绘制之间进行选择之类的操作将不起作用。但是,您可以通过使用自定义的
BindingAdapter
方法轻松解决此问题,例如,制作一个以
ListItemViewModel
为参数的绑定适配器,并将要设置的值类型的所有逻辑放在方法本身中。

状态为枚举,getState方法存在并返回特定状态enumI使用三元运算符
?:
和空合并
和混合类型数据做了可怕的事情。因此,数据绑定永远不会工作
<android.support.v7.widget.CardView
    xmlns:card_view="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="80dp"
    card_view:cardCornerRadius="4dp"
    android:foreground="@{viewModel.state != State.FINISHED? @color/gray_transparent54 : null}">
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@{viewModel.name}"
            tools:text="Name"
            android:textColor="@color/color_text_primary"
            android:textSize="24sp"/>
</android.support.v7.widget.CardView>