Skip to content

Commit

Permalink
[MaterialCardView] Support android:duplicateParentState.
Browse files Browse the repository at this point in the history
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
  • Loading branch information
Material Design Team authored and dsn5ft committed Sep 21, 2023
1 parent 2cfb127 commit 31af945
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 2 deletions.
Expand Up @@ -375,6 +375,7 @@ public void setClickable(boolean clickable) {
protected void onAttachedToWindow() {
super.onAttachedToWindow();

cardViewHelper.updateClickable();
MaterialShapeUtils.setParentAbsoluteElevation(this, cardViewHelper.getBackground());
}

Expand Down
Expand Up @@ -221,7 +221,7 @@ void loadFromAttributes(@NonNull TypedArray attributes) {

materialCardView.setBackgroundInternal(insetDrawable(bgDrawable));
fgDrawable =
materialCardView.isClickable() ? getClickableForeground() : foregroundContentDrawable;
shouldUseClickableForeground() ? getClickableForeground() : foregroundContentDrawable;
materialCardView.setForeground(insetDrawable(fgDrawable));
}

Expand Down Expand Up @@ -300,7 +300,7 @@ Rect getUserContentPadding() {
void updateClickable() {
Drawable previousFgDrawable = fgDrawable;
fgDrawable =
materialCardView.isClickable() ? getClickableForeground() : foregroundContentDrawable;
shouldUseClickableForeground() ? getClickableForeground() : foregroundContentDrawable;
if (previousFgDrawable != fgDrawable) {
updateInsetForeground(fgDrawable);
}
Expand Down Expand Up @@ -679,6 +679,17 @@ private float calculateCornerPaddingForCornerTreatment(CornerTreatment treatment
return 0;
}

private boolean shouldUseClickableForeground() {
if (materialCardView.isClickable()) {
return true;
}
View view = materialCardView;
while (view.isDuplicateParentStateEnabled() && view.getParent() instanceof View) {
view = (View) view.getParent();
}
return view.isClickable();
}

@NonNull
private Drawable getClickableForeground() {
if (rippleDrawable == null) {
Expand Down

0 comments on commit 31af945

Please sign in to comment.