android ripple无法处理RecyclerView项目

时间:2016-09-24 09:35:16

标签: android android-recyclerview ripple

我有一个包含四个项目的RecyclerView,并在RecyclerView项目布局的背景中应用了涟漪效果。当我单击该项目时,涟漪不显示。

categories.xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/category"
    android:clickable="true"
    android:background="@drawable/ripple_effect_2"
    android:layout_marginBottom="1dp"
   >

<ImageView
    android:layout_width="match_parent"
    android:layout_height="180dp"
    android:id="@+id/offimg"
    android:tint="#50000000"
    android:src="@drawable/electronics"
    android:layout_marginLeft="1dp"
    android:layout_marginRight="1dp"
    android:scaleType="fitXY"
    />
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Hotels"
        android:layout_marginLeft="5dp"
        android:layout_marginRight="5dp"
        android:gravity="center"
        android:textColor="#ffffff"
        android:textSize="20sp"
        android:textAllCaps="true"
        android:fontFamily="serif"
        android:id="@+id/textView1"
        android:layout_centerInParent="true"
        android:layout_centerHorizontal="true"
         />

</RelativeLayout>

ripple_effect_2.xml:

<?xml version="1.0" encoding="utf-8"?>
<ripple
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:color="#ffffff"
    >
    <item android:id="@android:id/mask"
        android:drawable="@color/semiTransparentBlack">

    </item>
</ripple>

1 个答案:

答案 0 :(得分:0)

如果您使用ConstraintLayout,请尝试

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

其他

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