2015-06-19 2 views
35

Ich versuche, Ripple-Effekt zu RecyclerView-Artikel hinzuzufügen. Ich habe online nachgesehen, konnte aber nicht finden, was ich brauche. Ich nehme an, dass es sich um einen benutzerdefinierten Effekt handelt. Ich habe android versucht: Hintergrund-Attribut auf die RecyclerView selbst und setzt sie auf „android: selectableItemBackground“, aber es hat nicht funktioniert .:Ripple-Effekt hinzufügen RecyclerView item

<android.support.v7.widget.RecyclerView 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:focusable="true" 
    android:clickable="true" 
    android:background="?android:selectableItemBackground" 
    android:id="@+id/recyclerView" 
    android:layout_below="@+id/tool_bar"/> 

Dies ist RecyclerView, dass ich den Effekt hinzufügen bin versucht zu: enter image description here

Antwort

91

Ich fand heraus. Das einzige, was ich tun musste, ist dieses Attribut hinzuzufügen:

android:background="?android:attr/selectableItemBackground" 

auf das Wurzelelement des Layouts, die meine RecyclerView Adapter wie das aufbläst:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
      xmlns:tools="http://schemas.android.com/tools" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:paddingTop="8dp" 
      android:paddingBottom="8dp" 
      android:background="?android:attr/selectableItemBackground" 
      tools:background="@drawable/bg_gradient"> 

<TextView 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:textSize="17sp" 
    android:layout_marginLeft="15dp" 
    android:layout_marginStart="15dp" 
    android:id="@+id/shoppingListItem" 
    android:hint="@string/enter_item_hint" 
    android:layout_centerVertical="true" 
    android:layout_alignParentLeft="true" 
    android:layout_alignParentStart="true"/> 

<CheckBox 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="@string/shopping_list_item_checkbox_label" 
    android:id="@+id/shoppingListCheckBox" 
    android:layout_centerVertical="true" 
    android:layout_marginRight="15dp" 
    android:layout_alignParentRight="true" 
    android:layout_alignParentEnd="true" 
    android:checked="false"/> 
</RelativeLayout> 

Ergebnis: enter image description here

+3

Was ist, wenn das Stammelement CardView ist? – Nabin

+6

@SpiderMan Sie sollten Ihren Code in ein Relatives oder ein lineares Layout schreiben und ihn dann in das Kartenlayout einfügen. – SanVed

+2

@pronoobsanved, das hat mir geholfen! Für diejenigen, die immer noch mit dem Problem konfrontiert sind, nimmt CardView ein einzelnes Layout als Kind. Für dieses Kind, setze clickable, fokussierbar als wahr und Hintergrund wie? Attr/selectedItemBackground –