Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

setAlpha() on onBindView in RecyclerView doesn't work on first display

Using the following code within a RecyclerView.Adapter:

onBindViewHolder(VH holder, int position){
   holder.itemView.setAlpha(0.5f);
}

Alpha will not be shown the first time the item is shown. However, if you leave the screen and come back, Alpha is then accurately shown. The value is being set, but not displayed until it's shown again. Any ideas on how to get setAlpha() to take effect on first viewing.

like image 611
Nic Capdevila Avatar asked Dec 02 '16 22:12

Nic Capdevila


Video Answer


1 Answers

After further investigation, this happens only when using an animator (such as the android.support.v7.widget.DefaultItemAnimator ) which will clear whatever alpha is set for the view. You can use

RecyclerView.setItemAnimator(null);

and alpha will remain set

like image 151
Nic Capdevila Avatar answered Oct 19 '22 17:10

Nic Capdevila