Search code examples
androidandroid-layoutandroid-recyclerviewandroid-viewholder

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.


Solution

  • 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