Rohan
Rohan

Reputation: 593

RecyclerView Item drawable states not working

I am using a Recyclerview. It has a GridLayoutManager and contains/displays a grid of images using the Glide/Picasso library. My item in the recyclerview has the following layout.

<?xml version="1.0" encoding="utf-8"?>
    <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/item_recycler_view"
android:clickable="true"
android:focusable="true"
android:foreground="@drawable/item_recycler_view">

<ImageView
    android:id="@+id/savedPhoto"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:adjustViewBounds="true"
    android:scaleType="centerCrop"
    android:transitionName="imageScale" />

I have set the following states drawable(item_recycler_view) to the parent FrameLayout.

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@android:color/holo_red_dark" android:state_selected="true" />
<item android:drawable="@android:color/holo_red_dark" android:state_pressed="true" />
<item android:drawable="@android:color/transparent" />

I change in state does not take place on pressing the item in the recyclerview.

How do I set different drawables for the different states of a RecyclerView item while continuing to use Picasso/Glide ??

Upvotes: 1

Views: 647

Answers (1)

Insoft
Insoft

Reputation: 548

I had same problem and I solved with android:state_focused="true" attribute in this way:

in your item_recycler_view

<?xml version="1.0" encoding="utf-8"?>
<ripple xmlns:android="http://schemas.android.com/apk/res/android"
    android:color="?attr/colorControlHighlight">
    <item>
        <selector xmlns:android="http://schemas.android.com/apk/res/android">
            <item
                android:drawable="@color/colorAccent"
                android:state_focused="true" />
        </selector>
    </item>
</ripple>

Upvotes: 1

Related Questions