24

当CardView在RecyclerView中时,有没有人在没有触摸反馈的情况下解决了它的奥秘?

我有一个 RecyclerView 和一堆 CardViews (一个CardList)。当我单击任何 CardView 时,我会启动另一个 Activity。这工作得很好,但是当我点击 CardView 时我看不到任何触摸反馈。

及时,我已经用这些配置了我的 CardView (XML):

android:clickable="true"
android:background="?android:selectableItemBackground"

谢谢!

4

5 回答 5

41

背景:

赞成的CardView忽略只能是颜色。边框和阴影实际上是背景的一部分,因此您无法自行设置。android:backgroundapp:cardBackground

解决方案:

使布局内部CardView可点击而不是卡片本身。您已经编写了此布局所需的两个属性:

android:clickable="true"
android:background="?android:selectableItemBackground"
于 2015-01-04T00:14:25.727 回答
8

解决方案 1

正如@Eugen 建议的那样,您可以使内部的布局CardView可点击,因此您可以使用android:background

<android.support.v7.widget.CardView
    ...
    android:clickable="true"
    android:background="?attr/selectableItemBackground">

解决方案 2

如果您不想通过使内部布局可点击而丢失项目点击侦听CardView器,您可以使用android:foreground

<android.support.v7.widget.CardView
    ...
    android:clickable="true"
    android:foreground="?attr/selectableItemBackground">

额外:如果您不想要矩形蒙版,您可以使用"?attr/selectableItemBackgroundBorderless"而不是。"?attr/selectableItemBackground"

于 2016-11-28T18:04:36.187 回答
1

创建选择器“drawable/card_foreground_selector”

<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="@dimen/card_radius" />
        </shape>
    </item>
    <item android:state_focused="true" android:state_enabled="true">
        <shape android:shape="rectangle">
            <solid android:color="#0f000000"/>
            <corners android:radius="@dimen/card_radius" />
        </shape>
    </item>
</selector>

并创建“drawable/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="3dp"
android:insetBottom="3dp"/>

修改您的项目 (item.xml)

<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/card_view"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:contentPadding="8dp"
    android:foreground="@drawable/card_foreground">

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

    // ..

    </LinearLayout>
</android.support.v7.widget.CardView>

你可以在这里查看原帖

于 2015-08-05T04:06:06.243 回答
1

添加foreground属性:

android:foreground="?android:attr/selectableItemBackground"
于 2016-12-15T13:58:03.247 回答
1

两种方法都应该以相同的方式工作。

1) 如果您希望 cardview 响应触摸反馈,请在cardview中使用这个。

 android:foreground="?android:attr/selectableItemBackground"
        android:clickable="true"
        android:focusable="true"

但如果上述方法不起作用,那么您可以在cardview的视图组(线性/相对等)上设置此属性。

   android:background="?android:attr/selectableItemBackground"
    android:clickable="true"
    android:focusable="true"

但是随后itemView的 ViewHolder 将不会响应触摸事件。由于它已被子视图使用,因此您必须在子视图上设置 clicklistener以进一步使用 recyclerview 适配器中的侦听器,这样我们可以在适配器中的 recyclerview 行项目上启用触摸和单击事件。

如果您很难触摸并单击带有波纹的卡片视图中的视图,那么这可能会有所帮助。触摸反馈问题

2.)第二种方法是使用自定义触摸选择器绘制并设置为背景的传统方式。

    <?xml version="1.0" encoding="utf-8"?>
<ripple
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:color="your ripple color">
    <item>
    <selector>
        <item android:state_selected="true">
            <color android:color="your selected color" />
        </item>
        <item android:state_activated="true">
            <color android:color="your selected color" />
        </item>
        <item>
            <color android:color="your normal color" />
        </item>
    </selector>
    </item>
</ripple>

Docs Ripple Drawable

于 2017-04-27T12:00:54.187 回答