2015-12-02 20 views
5

我有一個RecyclerView與項目佈局上啓用「selectableItemBackground」每個項目給我的觸摸漣漪效應。問題是,這些物品上有一個imageView,並且漣漪效應只發生在圖像下。如何在整個項目佈局上顯示漣漪(在imageView上),就像觸摸Google Play商店上的項目一樣?如何在Imageview上有selectableItemBackground?

+0

您應該將相同的內容應用於您的項目佈局作爲背景。你可以分享你的佈局。 – cgr

回答

7

或者你可以試試這個: -

<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

我試過了,selectableItemBackground在觸摸時沒有生效。基本上沒有任何反應。 – SergeantPeauts

+0

我編輯了代碼:)將'android:clickable =「true」'添加到'cardView'中。 – Wizard

+0

可愛的這個工作。除非你需要把注意力集中到真實。 – SergeantPeauts

0

嘗試在XML文件中使用android:drawSelectorOnTop="true"在RecyclerView。 並設置Ripple效果選擇器爲android:background屬性RecyclerView cell。選擇器的

實施例中可繪製-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> 

細胞和例如:

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

它仍然在單元格的imageView下。 – SergeantPeauts

+0

您是否已將'android:clickable =「true」 android:focusable =「true」'添加到您的父級佈局中? – mtwain

+0

這其實很好。但它只適用於api 21以上的版本嗎? – SergeantPeauts

1

我在​​propiety value ?android:attr/selectableItemBackground"中解決了父級內容中的設置並啓用了可調焦功能。

<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

不錯,使用前景你甚至可以添加不同的背景,並且「新聞」效果依然存在!非常感謝! –

相關問題