简体   繁体   中英

Recycler View Item not changing its colour

I had a recycler view and a grid layout manager to that.For the layout item inflated for recycler view I set a background colour like this

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:clickable="true"
    android:orientation="vertical">

    <TextView
        android:id="@+id/regNo"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="0.9"
        android:background="@drawable/student_li_bgcolor"
        android:gravity="center"
        android:padding="7dp"
        android:clickable="true"
        android:text="15BEC0584"
        android:textColor="#64DD17"
        android:textSize="17dp" />

</LinearLayout>

The content of student_li_bgcolor is this

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">

    <item
        android:state_selected="true">
        <shape>
            <solid
                android:color="#64DD17"
                />
        </shape>
    </item>
    <item
        android:state_pressed="true"
        >
        <shape>
            <solid
                android:color="#ffffff"
                />
        </shape>
    </item>
    <item
        android:state_selected="false"
        >
        <shape>
            <solid
                android:color="#ffffff"
                />
        </shape>
    </item>
</selector>

What I want to acheive is changing the item color to green if user has pressed it atleast once.

With this code

 <item
        android:state_pressed="true"
        >
        <shape>
            <solid
                android:color="#ffffff"
                />
        </shape>
    </item>
    <item
        android:state_pressed="false"
        >
        <shape>
            <solid
                android:color="#ffffff"
                />
        </shape>
    </item>

I'm able to change background colour on click.But its reverting back to its original colour as soon as user removed finger on it.I dont want that.Please help me...Thanks in advance...

If you want to change the background color permanently, you should add an OnClickListener and do it in OnClickListener.onClick() .

<selector> is just a state list resource that changes appearance depending on the button's current state.

How to use an onClickListener please refer to this .

I'm glad if it can help.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM