更改 RecyclerView 项目内 View 的背景

问题描述

在我的 recyclerView 中,我有一些项目,其中包含有关布局中项目的日期和其他项目信息。 问题是,当我尝试更改背景颜色时,不是针对所有项目,而是针对其某些内容,我遇到了一个更改其他项目背景的错误

我需要更改除日期之外的部分项目布局的背景。

代码有效,但更改了所有项目的背景颜色:

gif

    public void setItemContent(ReminderModel model,ArrayList<ReminderModel> reminders,int position) {
        if (position > 0) {
            ReminderModel prevIoUsReminder = reminders.get(position - 1);
            LocalDate prevIoUsDate = prevIoUsReminder.getDateTime().toLocalDate();
            LocalDate currentDate = model.getDateTime().toLocalDate();

            if (prevIoUsDate.compareto(currentDate) == 0) {
                date.setVisibility(View.GONE);
            } else {
                date.setVisibility(View.VISIBLE);
            }
        }

        if (position == 0) {
            date.setVisibility(View.VISIBLE);
        }

        this.reminderId = model.getId();

        final DateTimeFormatter dateFormatter = DateTimeFormatter.ofPattern("dd.MM.yy");
        final DateTimeFormatter timeFormatter = DateTimeFormatter.ofPattern("HH:mm");
        String dateString = dateFormatter.format(model.getDateTime());
        String timeString = timeFormatter.format(model.getDateTime());

        title.setText(model.getTitle());
        description.setText(model.getDescription());
        date.setText(dateString);
        time.setText(timeString);

        if (model.isSelected()) {
            itemView.setBackgroundColor(itemView.getResources().getColor(R.color.light_blue));
        } else {
            itemView.setBackgroundColor(itemView.getResources().getColor(android.R.color.transparent));
        }
    }

代码会根据需要更改背景颜色,但存在一个错误,即更改其他项目的背景颜色,而不管我什至没有触摸它们。

gif


    public void setItemContent(ReminderModel model,int position) {
        if (position > 0) {
            ReminderModel prevIoUsReminder = reminders.get(position - 1);
            LocalDate prevIoUsDate = prevIoUsReminder.getDateTime().toLocalDate();
            LocalDate currentDate = model.getDateTime().toLocalDate();

            if (prevIoUsDate.compareto(currentDate) == 0) {
                date.setVisibility(View.GONE);
            } else {
                date.setVisibility(View.VISIBLE);
            }
        }

        if (position == 0) {
            date.setVisibility(View.VISIBLE);
        }

        this.reminderId = model.getId();

        final DateTimeFormatter dateFormatter = DateTimeFormatter.ofPattern("dd.MM.yy");
        final DateTimeFormatter timeFormatter = DateTimeFormatter.ofPattern("HH:mm");
        String dateString = dateFormatter.format(model.getDateTime());
        String timeString = timeFormatter.format(model.getDateTime());

        title.setText(model.getTitle());
        description.setText(model.getDescription());
        date.setText(dateString);
        time.setText(timeString);

        if (model.isSelected()) {
            itemView.findViewById(R.id.container_reminder).setBackgroundColor(itemView.getResources().getColor(R.color.light_blue));
        } else {
            itemView.setBackgroundColor(itemView.getResources().getColor(android.R.color.transparent));
        }
    }

唯一的区别在于这一行:

itemView.findViewById(R.id.container_reminder).setBackgroundColor(itemView.getResources().getColor(R.color.light_blue));

其中 R.id.container_reminder 是项目布局的一部分,我需要更改背景。

项目布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    tools:context=".MainActivity">

    <TextView
        android:id="@+id/view_date"
        android:layout_width="match_parent"
        android:layout_height="30dp"
        android:layout_margin="8dp"
        android:layout_marginBottom="5dp"
        android:background="@mipmap/image_date_background"
        android:backgroundTint="#2C6991"
        android:fontFamily="@font/andika"
        android:gravity="center"
        android:textSize="18sp"
        android:textStyle="bold"
        tools:text="DATE" />

    <LinearLayout
        android:id="@+id/container_reminder"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="12dp"
        android:orientation="vertical"
        android:padding="10dp">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal">

            <TextView
                android:id="@+id/view_title"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginStart="4dp"
                android:layout_weight="1"
                android:fontFamily="@font/andika"
                android:textSize="18sp"
                android:textStyle="bold"
                tools:text="TITLE" />

            <TextView
                android:id="@+id/view_time"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginEnd="4dp"
                android:layout_weight="0.5"
                android:fontFamily="@font/andika"
                android:gravity="end"
                android:textSize="18sp"
                android:textStyle="bold"
                tools:text="TIME" />

        </LinearLayout>

        <TextView
            android:id="@+id/view_description"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginStart="4dp"
            android:fontFamily="@font/andika"
            android:textSize="16sp"
            android:textStyle="bold"
            tools:text="DESCRIPTION" />
    </LinearLayout>

</LinearLayout>

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)