Android-L CardView视觉触控反馈

任何人都可以向我解释如何实现一些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> 

API 11+

android:foreground="?android:attr/selectableItemBackground"CardView元素。

API 9+

android:foreground="?selectableItemBackground"到您的CardView元素。


编辑:源自中心而不是触摸点的纹波是一个已知的错误,并已被修复 。

为了正确地绘制前棒棒糖后棒棒糖上的select,您可以使用以下方法(想法是使用插入的可选的带圆angular的select器来预先棒棒糖 – 下面的示例使用自定义颜色,您可以将它们更改为默认值)

 android:foreground="@drawable/card_foreground" 

后棒棒糖

可绘制-V21 / card_foreground.xml

 <ripple xmlns:android="http://schemas.android.com/apk/res/android" android:color="#20000000" android:drawable="@drawable/card_foreground_selector" /> 

可绘制-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> 

预棒棒糖

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"/> 

绘制/ 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> 

这有助于我的情况

背景:

CardView忽略了android:background ,而只支持app:cardBackground ,它只能是彩色的。 边界和阴影实际上是背景的一部分,所以你不能自己设置。

解:

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

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

这是我的解决scheme。 它使用波纹棒棒糖+和前棒棒糖设备的静态前景。

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