CardView won't ripple on click

允我心安 提交于 2019-12-23 23:33:45

问题


In my android app I have a recyclerview containing card views. However for some reason when I long press the cardview the ripple effect appears but if I just click it, then the ripple effect doesn't appear. Any suggestions?

CardView Item Layout

<?xml version="1.0" encoding="utf-8"?>

<android.support.v7.widget.CardView xmlns:card_view="http://schemas.android.com/apk/res-auto"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/card_view"
    android:layout_width="fill_parent"
    android:layout_height="match_parent"
    android:layout_gravity="center"
    android:layout_margin="3dp"
    android:padding="3dp"
    card_view:cardCornerRadius="4dp"
    android:foreground="@drawable/card_foreground"
    android:background="@drawable/card_foreground"
    card_view:cardElevation="3dp">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical">

            <TextView
                android:id="@+id/title"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_alignParentLeft="true"
                android:layout_alignParentStart="true"
                android:layout_alignParentTop="true"
                android:text="New Text"
                android:textSize="20sp"
                android:textStyle="bold"
                android:typeface="normal" />

            <TextView
                android:id="@+id/author"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_alignParentLeft="true"
                android:layout_alignParentStart="true"
                android:layout_below="@+id/publishdate"
                android:text="New Text"
                android:textSize="15sp"
                android:typeface="normal" />

            <TextView
                android:id="@+id/publishdate"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_alignParentLeft="true"
                android:layout_alignParentStart="true"
                android:layout_below="@+id/title"
                android:text="New Text"
                android:textSize="15sp"
                android:typeface="normal" />
        </LinearLayout>
    </android.support.v7.widget.CardView>

card_foreground.xml

<inset xmlns:android="http://schemas.android.com/apk/res/android" android:drawable="@drawable/card_foreground_selector"
    android:insetLeft="2dp"
    android:insetRight="2dp"
    android:insetTop="4dp"
    android:insetBottom="4dp"/>

card_foreground_selector.xml

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="true">
        <shape android:shape="rectangle">
            <solid android:color="#18000000"/>
            <corners android:radius="4dp" />
        </shape>
    </item>
    <item android:state_focused="true" android:state_enabled="true">
        <shape android:shape="rectangle">
            <solid android:color="#0f000000"/>
            <corners android:radius="4dp" />
        </shape>
    </item>
</selector>

v21/card_foreground.xml

<ripple xmlns:android="http://schemas.android.com/apk/res/android" android:color="#20000000"
    android:drawable="@drawable/card_foreground_selector" />

v21/card_foreground_selector.xml

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="true">
        <shape android:shape="rectangle">
            <solid android:color="#18000000"/>
        </shape>
    </item>
    <item android:state_focused="true" android:state_enabled="true">
        <shape android:shape="rectangle">
            <solid android:color="#0f000000"/>
        </shape>
    </item>
</selector>

RecyclerView with Cardviews

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <ProgressBar
        android:id="@+id/progressBar"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        style="@style/Widget.AppCompat.ProgressBar" />

    <LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical">

        <android.support.v7.widget.RecyclerView
            android:id="@+id/list"
            android:scrollbars="vertical"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            style="@style/Widget.AppCompat.ProgressBar"/>

        <!-- empty view -->
    </LinearLayout>
</RelativeLayout>

来源:https://stackoverflow.com/questions/33883048/cardview-wont-ripple-on-click

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