Ripple effect over a RecyclerView item containing ImageView

后端 未结 6 1273
自闭症患者
自闭症患者 2020-12-12 22:09

I have a RecyclerView that expands the following grid item :



        
相关标签:
6条回答
  • 2020-12-12 22:39

    Change:

    android:background="?selectableItemBackground"
    

    To:

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

    And add this to your SquareImageView

    android:clickable="false" 
    
    0 讨论(0)
  • 2020-12-12 22:44

    If you have a CardView + ImageView
    just insert in CardView

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

    And for RelativeLayout + ImageView
    Insert in RelativeLayout

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

    Or

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

    This codes worked for me

    0 讨论(0)
  • 2020-12-12 22:53

    We can wrap RecyclerView Item inside FrameLayout and set android:foreground property to it. Checkout following link for details. It works for me pretty well.

    0 讨论(0)
  • 2020-12-12 22:55

    Above answers are correct. but I want to recommend the usage of android:foreground instead as it shows the ripple infront of the imageView.

    On your root view, add the following.

    android:foreground="@drawable/ripple_effect_color_primary"
    android:clickable="true"
    android:focusable="true"
    

    I know it's a pretty late answer to such old thread. But who knows, someone might actually find it useful.

    0 讨论(0)
  • 2020-12-12 22:56

    Your SquareImageView is filling up the entire space (width is match_parent and height is wrap_content) and so the SquareImageView receives the click event.

    What worked in my case was setting the clickable state of all objects in your RelativeLayout to false:

    android:clickable="false"
    

    Just make sure your RelativeLayout handles the click event in your activity. In my code I set the RelativeLayout to a view v and set an onClick Listener on it to handle the CheckBox within my List item.

    v.setOnClickListener(this);
    

    Hopefully this helps anyone who reads it.

    0 讨论(0)
  • 2020-12-12 23:03

    add below code to your parent layout

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

    if you want custom ripple effect add this ripple_custom.xml in your drawable-v21

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

    to support older version add ripple_custom.xml in drawable

    <?xml version="1.0" encoding="utf-8"?>
    <selector xmlns:android="http://schemas.android.com/apk/res/android">
        <item android:state_pressed="true">
            <shape android:shape="rectangle">
                <solid android:color="@color/colorHighlight" />
            </shape>
        </item>
        <item>
            <shape android:shape="rectangle">
                <solid android:color="@android:color/transparent" />
            </shape>
        </item>
    </selector>
    
    0 讨论(0)
提交回复
热议问题