Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RecyclerView onItemClick effect in L

Tags:

Maybe this question has been asked before, but I couldn't find a precise answer. I have implemented the RecyclerView widget as a list in my L test application and I want to get the 'material effect' when you click on an item form the list. I implemented an onClickListener in my ViewHolder creation and set the attribute

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

as a background to my list item layout (to the parent).

But none of this worked. When I click on the items NOTHING happens'. There is no effect, holo, material, none... Please point out if I am doing something wrong here... Thx

like image 659
Sandra Avatar asked Jul 25 '14 14:07

Sandra


2 Answers

Sandra's answer didn't work for me. I needed one more property in my list item layout:

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

Note: if you get an error when pasting in the last line, then your android app is set on version 10 or below. Just right click on the error in Android Studio and set it so it will create a v11 version of your layout as well. Then, in the original layout, make sure to delete

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

This is because the animation isn't supported in v10 versions of android or lower. With these two layout files set up, the animation will correctly show in v11+ versions of Android and of course it won't be shown in lower versions.

(or just increase the minSdkVersion version of your app to higher than 10)

like image 60
Micro Avatar answered Sep 19 '22 20:09

Micro


I made a silly mistake and did not put

android:clickable="true" android:focusable="true" 

to my list item layout. I think this was not required pre L, but it doesn't matter because that was the problem in this case.

like image 41
Sandra Avatar answered Sep 22 '22 20:09

Sandra