Ripple effect does not show up sometimes

后端 未结 2 2053
野的像风
野的像风 2021-02-07 07:38

I defined a ripple drawable and set it as the background for the listview item view\'s background. It almost works fine but the ripple effect just sometimes does not show up upo

相关标签:
2条回答
  • 2021-02-07 08:29

    OK, After some try and error, I have finally get it work.

    First, move the activated state out of ripple node:

    <?xml version="1.0" encoding="utf-8"?>
    <selector xmlns:android="http://schemas.android.com/apk/res/android">
        <item android:state_activated="true">
            <shape>
                <solid android:color="?android:attr/colorButtonNormal"/>
            </shape>
        </item>
        <item>
            <ripple android:color="?android:attr/colorControlHighlight">
                <item>
                    <shape>
                        <solid android:color="@android:color/transparent"/>
                    </shape>
                </item>
            </ripple>
        </item>
    </selector>
    

    After that, set drawSelectorOnTop to true for the ListView that use the drawable as item background.

    Now the ripple effect can work perfectly.

    0 讨论(0)
  • 2021-02-07 08:38

    In my case ripple effect is working after the first click, but for first click it didn't work for me. Have changed the background selector file with android:state_activated="true" and in main.xml android:clickable="true" then it's work fine for all time.

    selector.xml (under res\drawable\selector.xml)

    <?xml version="1.0" encoding="utf-8"?>
    <selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_activated="true" android:drawable="@drawable/card_bg_pressed" android:state_enabled="true" android:state_pressed="true"/>
    <item android:state_activated="true" android:drawable="@drawable/card_bg_focused" android:state_enabled="true" android:state_focused="true"/>
    <item android:state_activated="true" android:drawable="@drawable/card_bg_selected" android:state_enabled="false" android:state_selected="true"/>
    </selector>
    

    In activity_main.xml

     <com.mysample.RecyclingImageView
        android:id="@+id/imageview_overlay"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:background="@drawable/selector"
        android:clickable="true" />
    
    0 讨论(0)
提交回复
热议问题