问题
I've noticed that the effect created by the attribute ?attr/selectableItemBackground
only shows when I long tap the view. But I want it to be shown on every tap.
View is clickable and has on click listener.
How to do that?
回答1:
I've noticed there was a change in the behavior between Lollipop and Marshmallow:
- Lollipop - it would start the ripple on press.
- Marshmallow - the ripple starts on release.
Could that be the issue?
I would stick to the device Look & Feel but you could try this suggested solution:
https://stackoverflow.com/a/34167312/348378
Or use instead a library instead, maybe like this one:
https://github.com/balysv/material-ripple
回答2:
You can achieve this by setting the background of a view. First of all you have to make an item_selector.xml drawable in drawable and drawable-v21. For drawable folder-
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/item_pressed" android:state_focused="true" android:state_pressed="true"/>
<item android:drawable="@drawable/item_pressed" android:state_focused="false" android:state_pressed="true"/>
<item android:drawable="@drawable/item_normal" android:state_focused="true"/>
<item android:drawable="@drawable/item_normal" android:state_focused="false" android:state_pressed="false"/>
</selector>
For drawable-v21 folder-
<?xml version="1.0" encoding="utf-8"?>
<ripple xmlns:android="http://schemas.android.com/apk/res/android"
android:color="?android:attr/colorControlHighlight" >
<item>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:drawable="@drawable/item_pressed"
android:state_focused="true"
android:state_pressed="true"/>
<item
android:drawable="@drawable/item_pressed"
android:state_focused="false"
android:state_pressed="true"/>
<item
android:drawable="@drawable/item_normal"
android:state_focused="true"/>
<item
android:drawable="@drawable/item_normal"
android:state_focused="false"
android:state_pressed="false"/>
</selector>
</item>
</ripple>
Now all you need to do is put android:background="@drawable/item_selector" in the view tag. For example-
<LinearLayout
android:id="@+id/traveller_select_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/item_selector"
android:orientation="vertical"
android:paddingBottom="10dp"
android:paddingTop="5dp">
<TextView
android:id="@+id/textTravellers"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="20dp"
android:text="@string/passengers"
android:textColor="@color/baggage_grey"
android:textSize="12sp"/>
</LinearLayout>
Now make item_pressed.xml in the drawable folder-
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >
<solid android:color="@color/filter_bg"/>
</shape>
And the item_normal in the drawable folder-
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle" >
<solid android:color="@color/white"/>
</shape>
回答3:
Without your code, it's hard to know where goes wrong. But since I have just got this implemented, share with you what I've done, and perhaps you just follow, would help you figure out the issue.
In your view that need the click for ripple
<TextView
android:id="@+id/your_view_id"
android:layout_width="match_parent"
android:layout_height="wrap_content
android:test="Testing View"
android:background="@drawable/below_drawable">
In your /drawable-v21 folder, you have your below_drawable.xml with content
<?xml version="1.0" encoding="utf-8"?>
<ripple xmlns:android="http://schemas.android.com/apk/res/android"
android:color="@color/your_click_background_color">
<item android:id="@android:id/mask">
<shape android:shape="rectagle" >
<solid android:color="@android:color/your_mask_color" />
</shape>
</item>
</ripple>
Since the above for Lollipop (v21) only, if you want some impact to your non-lollipop (just show the color, but no ripple). You could have the below in /drawable folder the below_drawable.xml file.
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true">
<shape android:shape="rectangle">
<solid android:color="@color/your_click_background_color" />
</shape>
</item>
</selector>
Hope this helps.
来源:https://stackoverflow.com/questions/34467536/attr-selectableitembackground-effect-shows-only-on-longtap