MaterialCardView去除点击时的波纹效果

393 阅读1分钟
/**  
* @author: fuhejian  
* @date: 2023/7/4  
*/  
public class NoneRippleMaterialView extends MaterialCardView {  
  
  
public NoneRippleMaterialView(Context context) {  
this(context, null);  
}  
  
public NoneRippleMaterialView(Context context, AttributeSet attrs) {  
this(context, attrs, 0);  
}  
  
public NoneRippleMaterialView(Context context, AttributeSet attrs, int defStyleAttr) {  
super(context, attrs, defStyleAttr);  
}  
  
/**  
* 在xml设置clikable无效,设置setOnClickListener时生效  
* @param clickable true to make the view clickable, false otherwise  
*  
*/  
@Override  
public void setClickable(boolean clickable) {  
super.setClickable(clickable);  
Drawable foreground = getForeground();  
if (foreground instanceof InsetDrawable) {  
Drawable drawable = ((InsetDrawable) foreground).getDrawable();  
if (drawable instanceof LayerDrawable) {  
LayerDrawable layerDrawable = (LayerDrawable) drawable;  
if (layerDrawable.getNumberOfLayers() >= 2) {  
setForeground(layerDrawable.getDrawable(1));  
return;  
}  
}  
}  
//不可以点击时  
MaterialShapeDrawable materialShapeDrawable = new MaterialShapeDrawable();  
ShapeAppearanceModel shapeAppearanceModel = new ShapeAppearanceModel();  
shapeAppearanceModel.withCornerSize(getRadius());  
ColorStateList cardForegroundColor = getCardForegroundColor();  
ColorStateList colorStateList;  
colorStateList = cardForegroundColor;  
materialShapeDrawable.setFillColor(colorStateList);  
materialShapeDrawable.setShapeAppearanceModel(shapeAppearanceModel);  
materialShapeDrawable.setStroke(getStrokeWidth(), getStrokeColorStateList());  
setForeground(materialShapeDrawable);  
}  
}