24

RecyclerView内にあるときにタッチ フィードバックなしでCardViewの謎を解いた人はいますか?

CardViews ( CardList )の束を持つ RecyclerView があります。CardView をクリックすると、別のアクティビティが開始されます。それはかなりうまくいっていますが、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">

Extra :長方形マスクが必要ない場合は、"?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 に答える