Android-L CardView视觉触摸反馈

Smi*_*ler 64 android android-layout android-cardview android-5.0-lollipop

任何人都可以向我解释如何在CardView中实现Google I/O 2014中展示的一些视觉触摸反馈.

以下是我在XML中使用CardView的方法,可能有一些我想念的小东西,所以我只是想知道是否有人可以帮助我?

<!-- A CardView -->
<android.support.v7.widget.CardView
    xmlns:card_view="http://schemas.android.com/apk/res-auto"
    android:id="@+id/CardView_1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginLeft="10dp"
    android:layout_marginRight="10dp"
    android:layout_marginTop="10dp" 
    card_view:cardCornerRadius="4dp"
    android:elevation="2dp">

    <LinearLayout
        android:id="@+id/LinearLayout_1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:onClick="RunSomeMethod"">

    <!-- Main CardView Content In Here-->

    </LinearLayout> </android.support.v7.widget.CardView>
Run Code Online (Sandbox Code Playgroud)

nha*_*man 176

API 11+:

添加android:foreground="?android:attr/selectableItemBackground"到您的CardView元素.

API 9+:

添加android:foreground="?selectableItemBackground"到您的CardView元素.


编辑:源自中心而不是触摸点的波纹是已知的错误,并且已得到修复.

  • 你是对的.`android:clickable ="true"`也可以.根据源自按钮中心的动画,我认为这是一个错误. (8认同)
  • 关于前棒棒糖选择也显示阴影区域.这是预期的吗? (2认同)

Gre*_*ryK 35

要正确地绘制前LollipopLollipop之后的选择,您可以使用以下方法(想法是使用带有圆角的选择器的插入绘制用于预Lollipop - 下面的示例使用自定义颜色,您可以将它们更改为默认值):

android:foreground="@drawable/card_foreground"
Run Code Online (Sandbox Code Playgroud)

后棒棒糖

可绘制-V21/card_foreground.xml

<ripple xmlns:android="http://schemas.android.com/apk/res/android" android:color="#20000000"
        android:drawable="@drawable/card_foreground_selector" />
Run Code Online (Sandbox Code Playgroud)

可绘制-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>
Run Code Online (Sandbox Code Playgroud)

预棒棒糖

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="4dp"
    android:insetBottom="4dp"/>
Run Code Online (Sandbox Code Playgroud)

绘制/ 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="@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>
Run Code Online (Sandbox Code Playgroud)

  • 我认为这应该是"正确答案".它非常适合.谢谢! (4认同)

And*_*dyW 10

这有助于我的情况

背景:

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

解:

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

android:clickable="true"
android:background="?android:selectableItemBackground"
Run Code Online (Sandbox Code Playgroud)