2015-12-02 14 views
5

Tôi có một RecyclerView với mỗi mục có "selectableItemBackground" được bật trên bố cục mặt hàng để cho tôi hiệu ứng gợn sóng khi chạm vào. Vấn đề là, các mục có một imageView trên chúng và hiệu ứng gợn sóng chỉ diễn ra dưới hình ảnh. Làm thế nào để có gợn xuất hiện trên bố cục mục hoàn chỉnh (trên imageView), giống như khi chạm vào các mục trên Google Playstore?Làm cách nào để có selectableItemBackground trên Imageview?

+0

Bạn nên áp dụng giống như bố cục mặt hàng làm nền. Bạn có thể chia sẻ bố cục của mình. – cgr

Trả lời

7

Hoặc bạn có thể thử điều này: -

<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:layout_width="match_parent" 
     android:layout_height="200dp" 
     android:clickable="true" 
     android:focusable="true" 
     card_view:cardCornerRadius="4dp" 
     android:foreground="?android:attr/selectableItemBackground"> 

     <ImageView 
      android:id="@+id/imageView1" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" /> 
    </android.support.v7.widget.CardView> 
+0

Tôi đã thử điều này, selectableItemBackground không có hiệu lực khi chạm. Về cơ bản không có gì xảy ra. – SergeantPeauts

+0

Tôi đã chỉnh sửa mã :) Thêm 'android: clickable =" true "' vào 'cardView'. – Wizard

+0

đáng yêu làm việc này. Ngoại trừ bạn cần phải tập trung vào sự thật. – SergeantPeauts

0

Cố gắng sử dụng android:drawSelectorOnTop="true" trong RecyclerView của bạn trong tập tin XML. Và đặt bộ chọn hiệu ứng Ripple là android:background thuộc tính của bạn RecyclerView ô.

Ví dụ về chọn cho tế bào trong thư mục drawable-v21:

ripple_selector.xml

<ripple xmlns:android="http://schemas.android.com/apk/res/android" 
    android:color="@color/preference_item_pressed"> <!-- ripple color --> 
    <item android:id="@android:id/mask"> 
     <color android:color="@android:color/white" /> 
    </item> <!-- normal color --> 
</ripple> 

Và ví dụ về di động với bộ chọn nền:

cell.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="72dp" android:layout_height="72dp" 
    android:background="@drawable/ripple_selector"> 
</RelativeLayout> 
+0

Nó vẫn đi kèm theo imageView của ô. – SergeantPeauts

+0

Bạn đã thêm "android: clickable =" true " android: focusable =" true "' vào bố cục gốc của bạn? – mtwain

+0

Điều này thực sự tốt. Nhưng nó sẽ chỉ làm việc cho api 21 và ở trên vâng? – SergeantPeauts

1

Tôi đã giải quyết được đặt trong nội dung gốc ở foreground giá trị khuyến khích ?android:attr/selectableItemBackground" và có thể đặt tiêu điểm.

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:foreground="?android:attr/selectableItemBackground" 
     android:focusable="true"> 

     <ImageView 
      android:id="@+id/cover_image" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:adjustViewBounds="true" 
      android:contentDescription="@string/desc_empty" 
      android:scaleType="centerCrop" 
      android:src="@drawable/default_image" /> 

    </RelativeLayout> 
+1

Đẹp, sử dụng nền trước, bạn thậm chí có thể thêm một nền khác và hiệu ứng "báo chí" vẫn còn tồn tại! Cảm ơn rất nhiều! –

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