2015-06-19 14 views
35

Tôi đang cố gắng thêm Hiệu ứng Ripple vào mục của RecyclerView. Tôi đã có một cái nhìn trực tuyến, nhưng không thể tìm thấy những gì tôi cần. Tôi cho rằng nó phải là một hiệu ứng tùy chỉnh. Tôi đã thử android: attribute nền cho RecyclerView chính nó và đặt nó là "android:? SelectableItemBackground", nhưng nó đã không làm việc .:Thêm Hiệu ứng Ripple vào RecyclerView mục

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

Đây là RecyclerView mà tôi đang cố gắng để thêm các hiệu ứng để: enter image description here

Trả lời

91

Tôi đã tìm ra. Điều duy nhất mà tôi phải làm là thêm thuộc tính này:

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

đến phần tử gốc của cách bố trí mà bộ chuyển đổi RecyclerView tôi thổi phồng như thế:

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

Kết quả: enter image description here

+3

Nếu phần tử gốc là CardView thì sao? – Nabin

+6

@SpiderMan Bạn nên viết mã của bạn bên trong một Relative hoặc Linear Layout và sau đó đặt nó bên trong Layout Card. – SanVed

+2

@pronoobsanved, điều này đã giúp tôi! Đối với những người vẫn còn phải đối mặt với vấn đề, CardView có một bố trí duy nhất như một đứa trẻ. Đối với đứa trẻ đó, hãy đặt có thể nhấp, có thể đặt tiêu điểm là đúng và nền là? Attr/selectedableItemBackground –

Các vấn đề liên quan