【问题标题】:How can I have a ripple on click and an other foreground on a selected cardview?如何在选定的卡片视图上点击波纹和其他前景?
【发布时间】:2019-01-25 22:19:53
【问题描述】:

我在 ListView 中有多个 CardView。可以单击和选择这些卡片视图。单击时,我想显示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"
    >

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 cardview rippledrawable


    【解决方案1】:

    试试怎么样:

    可点击=真

    ?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:drawSelectorOnTop,但这也无济于事。 android:clickable 破坏了整个功能。我无法单击(并打开卡片视图),也无法通过长按选择卡片视图。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-06-05
    • 2015-06-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多