Android Lollipop CardView上的涟漪效应

Akr*_*tic 164 android android-layout android-cardview android-5.0-lollipop

我试图让CardView时,通过设置在Android触摸显示连锁反应:所描述的行为在XML文件中backgound属性这里 Android开发者页面上,但它不工作.根本没有动画,但是调用onClick中的方法.我也尝试过按照这里的建议创建一个ripple.xml文件,但结果相同.

CardView出现在活动的XML文件中:

<android.support.v7.widget.CardView
    xmlns:card_view="http://schemas.android.com/apk/res-auto"
    android:layout_width="155dp"
    android:layout_height="230dp"
    android:elevation="4dp"
    android:translationZ="5dp"
    android:clickable="true"
    android:focusable="true"
    android:focusableInTouchMode="true"
    android:onClick="showNotices"
    android:background="?android:attr/selectableItemBackground"
    android:id="@+id/notices_card"
    card_view:cardCornerRadius="2dp">

</android.support.v7.widget.CardView> 
Run Code Online (Sandbox Code Playgroud)

我对android开发比较陌生,所以我可能会犯一些明显的错误.
提前致谢.

Jad*_* Gu 523

您应该添加以下内容CardView:

android:foreground="?android:attr/selectableItemBackground"
android:clickable="true"
Run Code Online (Sandbox Code Playgroud)

  • 一点点观察:我注意到clickable属性不是必需的,甚至可以破坏一些东西!我有一个GridView充满了可点击的CardViews,听众没有正常工作.我从XML中删除了可点击标签,现在一切都完美无瑕 (14认同)
  • 适用于具有涟漪效果的棒棒糖,但在旧平台上运行时仅提供纯色.对我来说足够好了:) (8认同)
  • 没有`android:clickable ="true"`工作. (8认同)
  • 没有可点击的@joaquin,它会导致纹波仅来自中心 (2认同)
  • `?android:attr/selectableItemBackground`需要API级别11 (2认同)
  • SO中确实应该有一个系统,使用户可以覆盖已接受的答案。 (2认同)
  • 为了更好地使用CardView`?attr / selectableItemBackgroundBorderless` (2认同)

Abd*_*wan 29

将这两行代码添加到xml视图中,以便在cardView上产生连锁效果.

android:clickable="true"
android:foreground="?android:attr/selectableItemBackground"
Run Code Online (Sandbox Code Playgroud)


Rah*_*uja 24

我设法通过以下方式获得了对cardview的连锁反应:

<android.support.v7.widget.CardView 
    xmlns:card_view="http://schemas.android.com/apk/res-auto" 
    android:clickable="true" 
    android:foreground="@drawable/custom_bg"/>
Run Code Online (Sandbox Code Playgroud)

对于你可以在上面的代码中看到的custom_bg,你必须为lollipop(在drawable-v21包中)和pre-lollipop(在drawable包中)设备定义一个xml文件.对于drawable-v21包中的custom_bg,代码为:

<ripple 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:color="?android:attr/colorControlHighlight">
<item
    android:id="@android:id/mask"
    android:drawable="@android:color/white"/>
</ripple>
Run Code Online (Sandbox Code Playgroud)

对于drawable包中的custom_bg,代码是:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">

<item android:state_pressed="true">
    <shape>
        <solid android:color="@color/colorHighlight"></solid>
    </shape>
</item>
<item>
    <shape>
        <solid android:color="@color/navigation_drawer_background"></solid>
    </shape>
</item>
</selector>
Run Code Online (Sandbox Code Playgroud)

所以在预棒棒糖设备上你会有一个坚实的点击效果,而在棒棒糖设备上,你会对cardview产生连锁反应.

  • 嗨,我意识到通过在 CardView 的前景上应用选择器(或涟漪),它会阻止 CardView 本身的孩子 - http://stackoverflow.com/questions/33763403/cardviews-background-which-will-respond-to-androidstate-selected -and-androids 我可以知道你遇到和我一样的问题吗?或者,我错过了什么?谢谢。 (2认同)

Dev*_*aul 14

appcompat支持库中省略了涟漪效应,这是您正在使用的.如果你想看到涟漪使用Android L版本并在Android L设备上测试它.根据AppCompat v7站点:

"为什么前Lollipop没有涟漪?许多允许RippleDrawable顺利运行的是Android 5.0的新RenderThread.为了优化以前版本Android的性能,我们暂时离开了RippleDrawable."

看看这个链接点击这里获取更多信息

  • 但是你怎么会在针对Android Lollipop的cardView上产生涟漪效应呢? (39认同)

Fil*_*ito 9

如果minSdkVersion您正在使用的应用程序是9级,您可以使用:

android:foreground="?selectableItemBackground"
android:clickable="true"
Run Code Online (Sandbox Code Playgroud)

相反,从11级开始,您使用:

android:foreground="?android:attr/selectableItemBackground"
android:clickable="true"
Run Code Online (Sandbox Code Playgroud)

来自文档:

clickable - 定义此视图是否对单击事件做出反应.必须是布尔值,"true"或"false".

foreground - 定义要绘制内容的drawable.这可以用作叠加层.如果重力设置为填充,则前景drawable参与内容的填充.


Hit*_*wah 7

添加这两行类似的代码对于任何视图(如 Button、Linear Layout 或 CardView)来说就像魅力一样,只需放入这两行即可看到神奇的效果...

android:foreground="?android:attr/selectableItemBackground"
android:clickable="true"
Run Code Online (Sandbox Code Playgroud)


Pra*_*abs 6

对我来说,添加foregroundtoCardView不起作用(原因未知:/)

将相同的内容添加到它的子布局中就可以了。

代码:

<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:card_view="http://schemas.android.com/apk/res-auto"
    android:id="@+id/card_view"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:focusable="true"
    android:clickable="true"
    card_view:cardCornerRadius="@dimen/card_corner_radius"
    card_view:cardUseCompatPadding="true">

    <LinearLayout
        android:id="@+id/card_item"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:foreground="?android:attr/selectableItemBackground"
        android:padding="@dimen/card_padding">

    </LinearLayout>
</android.support.v7.widget.CardView>
Run Code Online (Sandbox Code Playgroud)

  • 那是因为孩子拦截了事件,如果您尝试向cardview添加填充(例如20dp),然后单击填充所在的部分,那么即使孩子没有,您也会看到涟漪效应前景属性集 (2认同)

Ank*_*pta 6

改用 Material Cardview,它扩展了 Cardview 并提供了多种新功能,包括默认的可点击效果:

<com.google.android.material.card.MaterialCardView>

...

</com.google.android.material.card.MaterialCardView>
Run Code Online (Sandbox Code Playgroud)

依赖(最多可用于 API 14 以支持旧设备):

implementation 'com.google.android.material:material:1.0.0'
Run Code Online (Sandbox Code Playgroud)