My RecyclerView displays a range of days like seen below.
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() {}
}