Skip to content

Commit 31af945

Browse files
Material Design Teamdsn5ft
Material Design Team
authored andcommittedSep 21, 2023
[MaterialCardView] Support android:duplicateParentState.
MaterialCardView hasn't shown the pressed ripple effect if the parent view is pressed and it has `android:duplicateParentState=true`, because its foreground drawable is not a RippleDrawable. This CL fixes the issue. PiperOrigin-RevId: 567117430
1 parent 2cfb127 commit 31af945

File tree

2 files changed

+14
-2
lines changed

2 files changed

+14
-2
lines changed
 

‎lib/java/com/google/android/material/card/MaterialCardView.java

+1
Original file line numberDiff line numberDiff line change
@@ -375,6 +375,7 @@ public void setClickable(boolean clickable) {
375375
protected void onAttachedToWindow() {
376376
super.onAttachedToWindow();
377377

378+
cardViewHelper.updateClickable();
378379
MaterialShapeUtils.setParentAbsoluteElevation(this, cardViewHelper.getBackground());
379380
}
380381

‎lib/java/com/google/android/material/card/MaterialCardViewHelper.java

+13-2
Original file line numberDiff line numberDiff line change
@@ -221,7 +221,7 @@ void loadFromAttributes(@NonNull TypedArray attributes) {
221221

222222
materialCardView.setBackgroundInternal(insetDrawable(bgDrawable));
223223
fgDrawable =
224-
materialCardView.isClickable() ? getClickableForeground() : foregroundContentDrawable;
224+
shouldUseClickableForeground() ? getClickableForeground() : foregroundContentDrawable;
225225
materialCardView.setForeground(insetDrawable(fgDrawable));
226226
}
227227

@@ -300,7 +300,7 @@ Rect getUserContentPadding() {
300300
void updateClickable() {
301301
Drawable previousFgDrawable = fgDrawable;
302302
fgDrawable =
303-
materialCardView.isClickable() ? getClickableForeground() : foregroundContentDrawable;
303+
shouldUseClickableForeground() ? getClickableForeground() : foregroundContentDrawable;
304304
if (previousFgDrawable != fgDrawable) {
305305
updateInsetForeground(fgDrawable);
306306
}
@@ -679,6 +679,17 @@ private float calculateCornerPaddingForCornerTreatment(CornerTreatment treatment
679679
return 0;
680680
}
681681

682+
private boolean shouldUseClickableForeground() {
683+
if (materialCardView.isClickable()) {
684+
return true;
685+
}
686+
View view = materialCardView;
687+
while (view.isDuplicateParentStateEnabled() && view.getParent() instanceof View) {
688+
view = (View) view.getParent();
689+
}
690+
return view.isClickable();
691+
}
692+
682693
@NonNull
683694
private Drawable getClickableForeground() {
684695
if (rippleDrawable == null) {

0 commit comments

Comments
 (0)
Please sign in to comment.