卡片视图 material 涟漪?

Card view material ripple?

我正在开发 material 设计界面。我有一些卡片,我希望在单击时产生连锁反应。我已经按照 google 所说的做了,但没有任何反应。这是我的代码:

<android.support.v7.widget.CardView
            xmlns:card_view="http://schemas.android.com/apk/res-auto"
            android:id="@+id/card_view"
            android:layout_gravity="center"
            android:clickable="true"
            android:layout_width="fill_parent"
            android:layout_height="250dp"
            android:elevation="5dp"
            android:background="?android:selectableItemBackground">
            >

            <ImageView 
                android:id="@+id/blue_image"
                android:src="@drawable/blue"
                android:layout_alignParentTop="true"
                android:layout_width="fill_parent"
                android:layout_height="200dp"
                android:elevation="8dp"
            />

            <TextView
                android:id="@+id/text_view"
                android:layout_width="match_parent"    
                android:layout_height="50dp"
                android:layout_alignParentBottom="true"
                android:text="And this is a blue card!" 
                android:elevation="10dp"
                />

看看我的例子 (API<21)。类似于棒棒糖波纹效果。

https://www.youtube.com/watch?v=liure-_hlX0&feature=youtu.be

对于这个演示,我使用了这个库: https://github.com/balysv/material-ripple

只需添加到 build.gradle:

 compile 'com.balysv:material-ripple:1.0.1'

卡片视图选择器(包装的 RelativeLayout):list_item_selector.xml:

<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- Pressed -->
<item android:drawable="@color/ripplecolorbackground" android:state_pressed="true"/>
<!-- selected -->
<item android:drawable="@color/ripplecolorbackground" android:state_selected="true"/>
<!-- focused -->
<item android:drawable="@color/ripplecolorbackground" android:state_focused="true"/>
<!-- default -->
<item android:drawable="@color/transparent"/>

布局:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="5dp"
tools:context=".MainActivity">

<android.support.v7.widget.CardView
    android:id="@+id/card_view"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_marginTop="5dp"
    android:layout_marginRight="5dp"
    android:layout_marginLeft="5dp"
    android:layout_marginBottom="5dp"
    card_view:cardUseCompatPadding="true"
    card_view:cardCornerRadius="3dp"
    card_view:cardElevation="3dp"
    card_view:cardPreventCornerOverlap="true">

    <com.balysv.materialripple.MaterialRippleLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:mrl_rippleColor="@color/rippplecolorlist"
        app:mrl_rippleAlpha="0.10"
        app:mrl_rippleOverlay="false"
        app:mrl_rippleHover="true"
        app:mrl_rippleDimension="1dp"
        app:mrl_rippleDuration="350">

    <RelativeLayout
        android:background="@drawable/list_item_selector"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>

            </com.balysv.materialripple.MaterialRippleLayout>
    </android.support.v7.widget.CardView>

颜色:

<color name="ripplecolorbackground">#1A000000</color>
<color name="rippplecolorlist">#26000000</color>

我创建了一个库,允许在不影响布局的情况下自定义每个视图以创建连锁反应

希望对你有帮助 https://github.com/xgc1986/RippleViews/blob/master/docs/RippleDrawableHelper.md

该示例还使用具有这种效果的 cardViews ;)