3

I need to delete ripple effect on a recycler view item click programmatically. This is because I reuse the adapter in several activities of my app, but there's a particular one where the items shouldn't be clickable, therefore I need to delete the ripple effect, otherwise it looks like it should do something on click, but it is not working.

The only way it works is by removing

android:foreground="?attr/selectableItemBackground"

on the view holder, but as I said, I can't do that because I'm reusing it, and it should show the ripple effect in several other activities. Is there a way to disable it from a particular activity?

Praveen P.
  • 976
  • 2
  • 11
  • 23

2 Answers2

1

You can do one thing. Instead of setting android:foreground by XML, You can set it programatically.

Whenever You do not need this foreground(ripple effect), simply set the null. And when you required this, then You can set this ?attr/selectableItemBackground programatically.

refer this to set it programatically

Bhargav Thanki
  • 4,924
  • 2
  • 37
  • 43
1

You should let your adapter take in a parameter that determines whether to display the ripple or not.

So start by changing your adapters constructor:

MyAdapter(boolean shouldRipple)

Then you can set the item foreground in onBindViewHolder:

onBindViewHolder {

    itemView.setForeground(...)
}
Henry Twist
  • 5,666
  • 3
  • 19
  • 44