CircleImageView:将条件值赋给xml属性bind:civ_border_color

时间:2017-02-09 12:16:41

标签: android layout binding

我正在尝试应用条件值

<?xml version="1.0" encoding="utf-8"?>

<data>

    <import type="android.view.View" />

    <variable
        name="viewModel"
        type="com.myproject.dialogs.MessageViewModel" />
</data>

<FrameLayout
    android:layout_width="@dimen/dialogs_messages_height_max"
    android:layout_height="@dimen/dialogs_messages_height_max">

    <TextView
        android:id="@+id/description_holder"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="@dimen/dialogs_messages_item_text_top_margin_at_max"
        android:ellipsize="end"
        android:gravity="center"
        android:maxLines="1"
        android:text="@{viewModel.userName}"
        android:textColor="@color/dark_primary_text"
        android:textSize="16sp"
        tools:text="Masha Petrova Vladimirovna" />

    <!-- when resizing messages - names goes under photo -->

    <de.hdodenhof.circleimageview.CircleImageView
        android:layout_width="96dp"
        android:id="@+id/photo"
        android:layout_height="96dp"
        android:layout_gravity="center"
        android:src="@drawable/empty_circle_photo"
        bind:civ_border_color="@{viewModel.isRead ? @color/red : @color/green}"
        bind:civ_border_width="3dp"
        bind:strokedAvatar="@{viewModel.avatarUrl}"
        android:layout_marginBottom="@dimen/base_double_indent"/>



</FrameLayout>

编译器发出错误

Error:(40, 38) Cannot find the setter for attribute 'bind:civ_border_color' with parameter type int on de.hdodenhof.circleimageview.CircleImageView. 

当我分配

bind:civ_border_color="@color/green"

一切都很顺利

我明确地错过了一些重要的事情。

这是viewModel类:

public class MessageViewModel extends BaseObservable {
    public final ObservableField<String> avatarUrl = new ObservableField<>();
    public final ObservableField<String> userName = new ObservableField<>();
    public final ObservableField<String> when = new ObservableField<>();
    public final ObservableField<Boolean> isRead = new ObservableField<>();
    public void setModel(DialogsList.Dialog model) {
        if (model != null) {
            User user = model.getUser();
            if (user != null) {
                Photo avatar = user.getAvatar();
                if (avatar != null) {
                    avatarUrl.set(avatar.getDefaultLink());
                }
                userName.set(user.getName());
                when.set(TimeUtils.getRelativeDate(model.getUpdateDate() * 1000L, true));
                isRead.set(true);
            }
        }
    }
}

1 个答案:

答案 0 :(得分:0)

您可以使用另一个名为color的变量来存储条件值

public class MessageViewModel extends BaseObservable {
    public final ObservableField<String> avatarUrl = new ObservableField<>();
    public final ObservableField<String> userName = new ObservableField<>();
    public final ObservableField<String> when = new ObservableField<>();
    public final ObservableField<Boolean> isRead = new ObservableField<>();
    int color = Color.GREEN;
    public void setModel(DialogsList.Dialog model) {
        if (model != null) {
            User user = model.getUser();
            if (user != null) {
                Photo avatar = user.getAvatar();
                if (avatar != null) {
                    avatarUrl.set(avatar.getDefaultLink());
                }
                userName.set(user.getName());
                when.set(TimeUtils.getRelativeDate(model.getUpdateDate() * 1000L, true));
                isRead.set(true);
                setColor(isRead ? Color.RED : Color.GREEN);
            }
        }
    }

    public int getColor() {
         return color;
    }

    public void setColor(int color) {
         this.color = color;
    }
}

XML文件中,使用该变量设置bind:civ_border_color

<de.hdodenhof.circleimageview.CircleImageView
        android:layout_width="96dp"
        android:id="@+id/photo"
        android:layout_height="96dp"
        android:layout_gravity="center"
        android:src="@drawable/empty_circle_photo"
        bind:civ_border_color="@{viewModel.color}"
        bind:civ_border_width="3dp"
        bind:strokedAvatar="@{viewModel.avatarUrl}"
        android:layout_marginBottom="@dimen/base_double_indent"/>