Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create a click animation for items in a RecycleView

I have been googling around to determine how I get a click animation for items in a RecycleView, something like "click/touch feedback".

I can see my items in the view, but there is no real reaction to when I press/click on them. I have a downsized version of this project:

https://github.com/writtmeyer/recyclerviewdemo/tree/master/app/src/main/java/com/grokkingandroid/samplesapp/samples/recyclerviewdemo

Does anyone know how to get click animations for items in a RecycleView?

like image 761
Ted Avatar asked Nov 16 '14 23:11

Ted


1 Answers

This is very easy to do. In the layout file of the view you're inflating for each row of your RecycleView, set the top-level view to be both clickable and focusable. Then, set the background of that view to ?android:attr/selectableItemBackground. Based on the GitHub project you posted, it looks like the view you're inflating for each row is item_demo_01.xml. So, that layout would now look something like this:

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/container_list_item"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:clickable="true"
    android:focusable="true"
    android:background="?android:attr/selectableItemBackground">

    <include layout="@layout/common_item_layout" />

</RelativeLayout>
like image 159
Nathan Walters Avatar answered Nov 03 '22 01:11

Nathan Walters