android - 我怎样才能在选定的卡片 View 上点击时出现涟漪和其他前景?

标签 android cardview rippledrawable

我在 ListView 中有多个 CardView。可以单击并选择这些卡片 View 。单击时,我想显示一个 ripple。选择后,我想更改 foreground。目前为两部作品之一。如何让它们同时工作?

我尝试过各种不同的版本。戴面具和不戴面具,设置所有状态,只有一个。问题是有 4 种可能的结果。

1:什么都不显示
2:只显示纹波
3:选择cardview后只有前景变色
4:显示波纹,但即使按下,前景也会改变颜色,未选中时不会变回。

ListView 项目:

<android.support.v7.widget.CardView
    android:id="@+id/card"
    android:layout_width="match_parent"
    android:layout_height="280dp"
    app:cardElevation="4dp"
    app:cardCornerRadius="4dp"
    android:layout_marginLeft="20dp"
    android:layout_marginRight="20dp"
    android:layout_marginTop="5dp"
    android:layout_marginBottom="5dp"
    android:foreground="@drawable/rowselector"
    >

行选择器.xml文件:

<?xml version="1.0" encoding="utf-8"?>
<ripple xmlns:android="http://schemas.android.com/apk/res/android" android:color="#20000000">
    <item android:drawable="@drawable/card_foreground_selector" />
</ripple>

card_foreground_selector.xml 文件:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_selected="true">
        <shape android:shape="rectangle">
            <solid android:color="#aae3f7ee" />
        </shape>
    </item>
    <item android:state_focused="true">
        <shape android:shape="rectangle">
            <solid android:color="#aae3f7ee" />
        </shape>
    </item>
    <item>
        <shape android:shape="rectangle">
            <solid android:color="#00000000" />
        </shape>
    </item>
</selector>

我认为这会在按下时显示波纹,在长按/选择 CardView 时会显示不同的 foreground 颜色。 ripple 当前缺失。

最佳答案

试试怎么样:

可点击=真

?android:attr/selectableItemBackground

要将波纹效果应用于背景,同时使用前景在选择项目后更改颜色?

<android.support.v7.widget.CardView
    android:id="@+id/card"
    android:layout_width="match_parent"
    android:layout_height="280dp"
    app:cardElevation="4dp"
    app:cardCornerRadius="4dp"
    android:layout_marginLeft="20dp"
    android:layout_marginRight="20dp"
    android:layout_marginTop="5dp"
    android:layout_marginBottom="5dp"
    android:clickable="true"
    android:background="?android:attr/selectableItemBackground"
    android:foreground="@drawable/card_foreground_selector">

关于android - 我怎样才能在选定的卡片 View 上点击时出现涟漪和其他前景?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54373546/

相关文章:

android - 对长按执行涟漪效应

android - 在 Android 中收到一些通知后自动启动应用程序

java - 如何以编程方式在android中创建checkedtextview?

android-cardview - Cardview 高程不适用于 androidx 工件

android - 有没有办法用我的 Json 数据设置可绘制文件夹中的图像,该数据包括可绘制文件夹中图像的名称

Android - 将一个 Drawable 设置为多个按钮

android - 将 RippleDrawable 和 StateListDrawable 的效果添加到 RecyclerView

java - 在 alertDialog 中显示 Recyclerview

java - 如何解析从php返回的数组到android中的String数组

android - 网格中的 CardView 不在所有侧面显示阴影