1

My RecyclerView displays a range of days like seen below.

Screenshot

At first, everything is working as intended (dates, color, abbreviation, and transparency are set correctly). When I press one of the circles below the data of the centered day is supposed to change. And it does, except that the transparency is set to 1 (opaque) regardless of the data. If you scroll a bit until the view, which was supposed to have transparency, is recycled and then go back, the transparency is set correctly again.

This is my RecyclerView Adapter:

public class RecyclerViewAdapterDays extends RecyclerView.Adapter<RecyclerViewAdapterDays.ViewHolder> {
    private final int[] month;
    private final int[] dates;
    private final boolean[] weekends;
    private final Shift[] shifts;
    private int default_background_color;

    public RecyclerViewAdapterDays(int[] month, int[] dates, Shift[] shifts, boolean[] weekends) {
        this.month = month;
        this.dates = dates;
        this.shifts = shifts;
        this.weekends = weekends;
    }

    @Override
    public RecyclerViewAdapterDays.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        DoubledotLayoutBinding binding = DataBindingUtil.inflate(LayoutInflater.from(parent.getContext()), R.layout.doubledot_layout, parent, false);
        ViewHolder viewHolder = new ViewHolder(binding);
        default_background_color = parent.getContext().getResources().getColor(R.color.colorPrimaryDark);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        System.out.println("Binding " + position);
        System.out.println("Alpha " +  (weekends[position] ? 0.2f : 1f));
        holder.binding.setShift(shifts[month[position]]);
        holder.binding.setDate(String.valueOf(dates[position]));
        System.out.println("Date " +  (dates[position]));
        holder.binding.setDefaultColor(default_background_color);
        holder.binding.rootLayout.setAlpha(weekends[position] ? 0.2f : 1f);
        holder.binding.executePendingBindings();
    }

    @Override
    public int getItemCount() {
        return month.length;
    }

    public static class ViewHolder extends RecyclerView.ViewHolder {
        final DoubledotLayoutBinding binding;

        public ViewHolder(DoubledotLayoutBinding itemView) {
            super(itemView.getRoot());
            binding = itemView;
        }
    }
}

If I run the app and press one of the circles which will change a shift in the shifts array and then calls notifyItemChanged. (nether dates nor weekends will change) I can see in the log that onBindViewHolder was called on the correct position and that weekends[position] ? 0.2f : 1f indeed returned 0.2f.

So why is my RecyclerView item still opaque?

Edit: Here is the layout for the days:

<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools">

    <data>

        <variable
            name="shift"
            type="hexpoly.dienstplan.Shift" />

        <variable
            name="date"
            type="String" />

        <variable
            name="defaultColor"
            type="Integer" />
    </data>

    <android.support.constraint.ConstraintLayout
        android:id="@+id/root_layout"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:paddingLeft="@dimen/activity_edit_month_doubledot_padding_land"
        android:paddingRight="@dimen/activity_edit_month_doubledot_padding_land"
        android:paddingTop="8dp"
        android:paddingBottom="8dp">

        <ImageView
            android:layout_width="@dimen/activity_edit_month_doubledot_width"
            android:layout_height="80dp"
            android:src="@drawable/doubledot"
            android:tint="@{shift.color == 0 ? defaultColor : shift.color}"
            tools:ignore="ContentDescription" />

        <android.support.constraint.Guideline
            android:id="@+id/guideline"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            app:layout_constraintGuide_percent="0.5" />

        <TextView
            android:id="@+id/tv_day"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@{date, default = 12}"
            android:textAppearance="@style/TextAppearance.AppCompat.Large"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toStartOf="@id/guideline"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

        <TextView
            android:id="@+id/tv_shift"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:text="@{shift.short_name, default = F1}"
            android:textAppearance="@style/TextAppearance.AppCompat.Large"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="@id/guideline"
            app:layout_constraintTop_toTopOf="parent" />

    </android.support.constraint.ConstraintLayout>

</layout>

And the shift class passed to the layout:

public class Shift
{
    public String short_name, name, start, end;
    public int color, id;
    public boolean priority = false;
    public boolean defaultShift = false;

    Shift() {}

}
Joschka Goes
  • 103
  • 1
  • 6
  • 1
    You might want to add the layout as well. – tynn May 02 '17 at 07:03
  • Did you ever find the answer to this? I think I'm having a similar bug – Oblivionkey3 Aug 10 '17 at 14:37
  • @Oblivionkey3 sadly I still have no solution for this isue. Can you describe your problem maybe that will help. – Joschka Goes Aug 12 '17 at 07:41
  • 1
    I have a workaround for this problem - in `onBindViewHolder` I call `holder.setIsRecyclable(false);` - this solved my issue, although it is not a great solution as it negates one of the major optimizations provided in a RecyclerView. – Oblivionkey3 Aug 12 '17 at 22:25

0 Answers0