No ripples for MaterialCardView

不羁的心 提交于 2019-12-04 17:23:13

问题


Are there no ripples on the new MaterialCardView?

I used CardView with android:clickable="true" and android:foreground="?android:attr/selectableItemBackground" which worked fine but with the new MaterialCardView there are no ripples at all 😰.

Other Views etc. keep working but I want my card to be clickable (with ripple as feedback).


回答1:


This is a bug with MaterialCardView. It's overriding the foreground after it's created. I've filled a bug. It should be fixed to show a ripple if the card is clickable without needing to set the foreground.

If you can't wait for a fix you could use CardView from the support lib, or you could change your code to set the foreground drawable after the CardView has been inflated, or we would gladly accept a pull request! Relevant code at: https://github.com/material-components/material-components-android/blob/master/lib/java/com/google/android/material/card/MaterialCardView.java. It can probably use https://github.com/material-components/material-components-android/blob/master/lib/java/com/google/android/material/ripple/RippleUtils.java and a RippleDrawable for SDK >= 21.




回答2:


In my case, It worked after adding android:clickable="true" and card_view:rippleColor="#cfd8dc" in XML file. Hope it will help to future SO.

<com.google.android.material.card.MaterialCardView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
card_view:cardElevation="2dp"
card_view:rippleColor="#cfd8dc"
android:clickable="true"
card_view:cardCornerRadius="5dp"
card_view:cardUseCompatPadding="true"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android">



回答3:


Ripple effect is working now, but only if you add an OnClickListener on the CardView



来源:https://stackoverflow.com/questions/50675934/no-ripples-for-materialcardview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!