android - 错误的文本颜色绑定(bind)到 Viewholder

标签 android android-recyclerview

我有一个 RecyclerView,显示带有时间戳的项目列表。我希望每个查看器中的一个 TextView 的文本根据每个项目的时间戳是不同的颜色。 Viewholder 似乎在日志中正确绑定(bind),并且正确的颜色显示在 Android Studio 的左侧 Pane 中,但设备上的文本颜色是错误的:

enter image description here

这是我的适配器的相关代码:

 @Override
    public void onBindViewHolder(@NonNull ChatViewHolder holder, int position) {

    Log.d(TAG, "OnbindViewholder Called");
    Message message = messageList.get(position);
    int authorColor = getAuthorColor(Calendar.getInstance().getTime(), message.getTimestamp());

    Log.d(TAG,Integer.toString(authorColor));

    holder.message.setText(message.getMessage());
    holder.author.setText(message.getAuthor() + ":");
    holder.author.setTextColor(authorColor);

}

public class ChatViewHolder extends RecyclerView.ViewHolder {

    TextView author;
    TextView message;
    RelativeLayout singleMessageContainer;

    public ChatViewHolder(View itemView) {
        super(itemView);

        author = itemView.findViewById(R.id.chatAuthor);
        message = itemView.findViewById(R.id.chatMessage);
        singleMessageContainer = itemView.findViewById(R.id.singleMessageContainer);
        author.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                createDMActivity(author.getText().toString());
            }
        });

    }
}

private int getAuthorColor(Date currentDate, Date messageDate) {
    int authorColorResourceID = 1;
    long timeAgo = currentDate.getTime() / 60000 - messageDate.getTime() / 60000;

    Log.d(TAG, "Time Ago = " + Long.toString(timeAgo));

    if (timeAgo < 15) {
        authorColorResourceID = R.color.Accent3;

    }
    if (timeAgo >= 15 && timeAgo < 30) {
        authorColorResourceID = R.color.chatLast30;
    }

    if (timeAgo >= 30 && timeAgo < 60) {
        authorColorResourceID = R.color.chatLastHour;
    }
    if (timeAgo >= 60 && timeAgo < 180) {
        authorColorResourceID = R.color.chatLast3Hours;
    }
    if (timeAgo >= 180) {
        authorColorResourceID = R.color.chatOver3Hours;
    }

    Log.d(TAG, Integer.toString(authorColorResourceID));


    return authorColorResourceID;

}

还有我的 colors.xml 文件:

<resources>
<color name="colorPrimary">#6247aa</color>
<color name="colorPrimaryDark">#1b1721</color>
<color name="colorAccent">#bdede0</color>

<color name="yellow">#f1c40f</color>
<color name="white">#ffffff</color>
<color name="black">#000000</color>
<color name="green">#27AE60</color>
<color name="light_purple">#E8E1E7</color>
<color name="light_green">#50d050</color>
<color name="dark_green">#008000</color>
<color name="Accent">#bdede0</color>
<color name="lightBackground">#e0e0e0</color>
<color name="text_color_primary">#ddffffff</color>
<color name="hintColor">#999999</color>
<color name="Accent2">#bb90fe</color>
<color name="Accent3">#55d6be</color>
<color name="chatLast30">#3f9b89</color>
<color name="chatLastHour">#2e7164</color>
<color name="chatLast3Hours">#24584e</color>
<color name="chatOver3Hours">#1c413b</color>

最佳答案

TextView.setTextColor() 接受一个颜色值。您正在向它传递一个颜色资源 ID。不幸的是,两者都表示为 int,因此编译器无法判断是否犯了这个错误。

要解决此问题,请在调用 setTextColor() 之前将颜色资源 ID 解析为颜色值:

int authorColorId = getAuthorColor(Calendar.getInstance().getTime(), message.getTimestamp());
int authorColor = ContextCompat.getColor(holder.itemView.getContext(), authorColorId);
holder.author.setTextColor(authorColor);

关于android - 错误的文本颜色绑定(bind)到 Viewholder,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52997980/

相关文章:

android - 更改视频聊天帧率 tokbox

android - LinearLayout subview 推送其他 View

java - 如何避免Runnable回调泄漏?

android - 在三星设备上继续运行时异常 : android. view.DisplayListCanvas.throwIfCannotDraw

java - 自定义 RecyclerAdapter 和 startActivityForResult

android - RecyclerView 可扩展的 cardView

android - 如何在 android 中使用 Text Watcher 从 Recycler View 适配器获取编辑文本位置

Android - 从 Android 应用程序更新 Facebook 状态

Android recyclerview-selection 实现?

Android:如何在RecyclerView中获取复选框的选中项以将其发送到服务器