Touch feedback with RecyclerView and CardView
Solution 1:
Assuming you are using Material/Appcompat theme and Lollipop,I got this to work by making the CardView have the following attributes:
android:focusable="true"
android:clickable="true"
android:foreground="?android:attr/selectableItemBackground"
Solution 2:
CardLayout is just a subclass of ViewGroup, So setting:
android:background="?android:attr/selectableItemBackground"
should do the trick.
UPDATE:
(looks like you are trying to use a custom ripple color.)
Try using a ripple drawable with custom color as the background (I haven't used this, So I cannot verify this behavior.) see: the documentation or this question to get you started.
Solution 3:
for me:
android:background="?android:attr/selectableItemBackground"
made it finally work. see background of recyclerView / content behind the item, and also see the ripple effect used with recyclerView.
Solution 4:
None of the above answers worked for me or was either too complicated.
Then I realized I had to set the following properties in THE RECYCLERVIEW ADAPTER LAYOUT.
android:background="?android:attr/selectableItemBackground"
android:focusable="true"
android:clickable="true"
Then I got it to work.
Solution 5:
I was now able to solve the issue.
The problem is that i use onClickListener on the TextViews and this click listener prevents the RippleForeground from being notified about the touch event. So the solution is to implement an TouchListener on those TextViews and pass through the touch event.
The class is really simple, and can be used everywhere:
package com.mikepenz.aboutlibraries.util;
import android.support.v7.widget.CardView;
import android.view.MotionEvent;
import android.view.View;
/**
* Created by mikepenz on 16.04.15.
*/
public class RippleForegroundListener implements View.OnTouchListener {
CardView cardView;
public RippleForegroundListener setCardView(CardView cardView) {
this.cardView = cardView;
return this;
}
@Override
public boolean onTouch(View v, MotionEvent event) {
// Convert to card view coordinates. Assumes the host view is
// a direct child and the card view is not scrollable.
float x = event.getX() + v.getLeft();
float y = event.getY() + v.getTop();
if (android.os.Build.VERSION.SDK_INT >= 21) {
// Simulate motion on the card view.
cardView.drawableHotspotChanged(x, y);
}
// Simulate pressed state on the card view.
switch (event.getActionMasked()) {
case MotionEvent.ACTION_DOWN:
cardView.setPressed(true);
break;
case MotionEvent.ACTION_UP:
case MotionEvent.ACTION_CANCEL:
cardView.setPressed(false);
break;
}
// Pass all events through to the host view.
return false;
}
}
It can be used by adding this as TouchListener:
RippleForegroundListener rippleForegroundListener = new RippleForegroundListener();
older.libraryCreator.setOnTouchListener(rippleForegroundListener);
This listener will just pass through the touch event to the CardView and trigger the correct Ripple effect. (You can also modify this to take any view. It should not be limited to a CardView)