10

I am trying to change the transparency of item-views in a RecyclerView according to certain user inputs.

if (quantity>0) {
    holder.itemView.setAlpha((float) 1);
} else {
    holder.itemView.setAlpha((float) 0.65);
}

Changing alpha from 0.65 to 1 works fine when quantity > 0. But the reverse is not working on the other case. When debugging, it clearly shows going through the line holder.itemView.setAlpha((float) 0.65); However, alpha is not reduced. Any clue about what's going on?

Bertram Gilfoyle
  • 9,899
  • 6
  • 42
  • 67
  • 3
    Why weird `(float) 1` instead of `1f` (or even better, `1.0f`)? – Pawel Jul 13 '18 at 14:01
  • 1
    *it clearly shows going through the line holder.itemView.setAlpha((float) 0.65)* when it shouldn't? –  Jul 13 '18 at 14:07
  • @mTak no. Only when it should do. But it's not making any change in the alpha. That's what I am taking about. – Bertram Gilfoyle Jul 13 '18 at 16:44
  • Explain this: *it clearly shows going through the line holder.itemView.setAlpha((float) 0.65); * this is *Changing alpha from 1 to 0.65* that *works fine*. Does the debugger go through the other line? –  Jul 13 '18 at 19:09
  • @mTak Really sorry. There was a mistake in the question (At the bold part ) . I have updated it. – Bertram Gilfoyle Jul 16 '18 at 07:08

4 Answers4

8

recycler's ItemAnimator changes alpha during update item process you can try to add

((SimpleItemAnimator) myRecyclerView.getItemAnimator()).setSupportsChangeAnimations(false);
  • RecyclerView.setItemAnimator(null); worked for me. I was about to post the answer. But while using your method, I could preserve other animations. Thank you very much. – Bertram Gilfoyle Jul 16 '18 at 08:25
  • I was stuck on this issue for quite sometime. Thank you. Thank you so much. – Ravi Rawal May 16 '19 at 11:57
8

I had this same problem. Instead of changing the alpha of the itemView, give an name to your root layout and change its alpha instead, as the recyclerview animations handle the itemView alpha animation making it not work.

Tgo1014
  • 536
  • 1
  • 7
  • 17
  • 1
    It was an old question and the previous [answer](https://stackoverflow.com/a/51356316/7594961) worked for me. I couldn't try this however this seems to be a better solution if it works. – Bertram Gilfoyle Jun 26 '19 at 12:19
  • 1
    Thanks for the reply @Anees. I knew it was an old question, but I just had this problem today and found your question. The solution here worked but it removed the diff animations. As I solved it without removing the animation I thought it would be nice to leave the reply here for someone in the future :) – Tgo1014 Jun 26 '19 at 13:13
  • 1
    This is not working, because the itemView is your root layout. – Kimi Chiu May 23 '20 at 16:30
  • 1
    @KimiChiu I didn't try this method but I think by root view, he means the direct child of itemview. If there is more than one you have to wrap it in another layout. – Bertram Gilfoyle May 23 '20 at 17:25
5

Remove item animator

In Java:

mRecyclerView.setItemAnimator(null);

Or in Kotlin:

recycler_view.itemAnimator = null
Boken
  • 4,825
  • 10
  • 32
  • 42
JackWu
  • 1,036
  • 1
  • 12
  • 22
1

Consider this is the HolderView class

class MyViewHolder(val viewHolder: View) : RecyclerView.ViewHolder(view)

And consider this is how your Adapter class looks like from inside

// ...

override fun onBindViewHolder(holder: MyViewHolder, i: Int) {
    holder.viewHolder.alpha = 0.65f
}

Sometimes if your code is holder.viewHolder.alpha = 0.65f it doesn't work always!

Rather than that, you may use alpha of the main container just like that

<RelativeLayout
    android:id="@+id/viewMain"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="horizontal">

    ... Your other components goes here

</RelativeLayout>

Now, from your adapter use this instead, it should work in all cases

// ...

override fun onBindViewHolder(holder: MyViewHolder, i: Int) {
    holder.viewMain.alpha = 0.65f
}
Osama Remlawi
  • 2,356
  • 20
  • 21