更改recyclerview项内视图的背景

6ovsh4lw  于 2021-06-29  发布在  Java
关注(0)|答案(0)|浏览(180)

在我的recyclerview中,我有一些包含日期和布局中某个项目的其他项目信息的项目。问题是,当我尝试更改背景颜色时,并不是针对所有项目,而是仅针对其中的一些内容,我有一个bug,它会更改其他项目的背景。
我需要改变除日期以外的部分项目布局的背景。
此代码有效,但会更改所有项目的背景色:

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));
        }
    }

这段代码会根据需要更改背景色,但有一个bug,更改其他项目的背景色,而我甚至不碰它们。

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.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.u提醒是项目布局的一部分,我需要更改背景。
项目布局

<?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>

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题