How to set different colors of pressed states for each list item?

In fact, I am developing an Android application that has several watchlists. In a ListView implementation, I inflate a cell for each list item. This is category_cell.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="60dip"
    android:id="@+id/cell_layout"
    android:background="@drawable/list_bg">
    <RelativeLayout
        android:id="@+id/category_cell_layout"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent">
        <ImageView
            android:id="@+id/category_image"
            android:layout_centerVertical="true"
            android:layout_marginLeft="10dip"
            android:layout_height="45dip"
            android:layout_width="45dip" />
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/category_name"
            android:text="Category Name"
            android:textSize="16dip"
            android:typeface="sans"
            android:layout_centerVertical="true"
            android:layout_marginLeft="70dip"
            android:textColor="@color/white" />
        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/arrow_image"
            android:background="@drawable/list_arrow"
            android:layout_alignParentRight="true"
            android:layout_centerVertical="true"
            android:layout_marginRight="10dip" />
    </RelativeLayout>
</RelativeLayout>

The xml label is placed on the background of this cell. that list_bg.xml has the following code:

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

When filling out the list using elements, I want to set a different color for the pressed and focused states of the background xml for each element. Each element contains a color value, and I want to set this color in the pressed state of this element.

StateListDrawable states = new StateListDrawable();
states.addState(new int[] {android.R.attr.state_pressed},getResources().getDrawable(R.color.translucent_red));
states.addState(new int[] {android.R.attr.state_focused},getResources().getDrawable(R.color.white));
states.addState(new int[] { },getResources().getDrawable(R.color.white));
row.setBackgroundDrawable(states);

I tried to implement this, but it usually takes drawable as the second add_state parameter, but I want to post the color there .... Can someone help me ???

+5
1

list_bg.xml. ,

<selector xmlns:android="http://schemas.android.com/apk/res/android">

<item android:state_pressed="true"><shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
       <gradient android:angle="90" android:centerColor="#6da23f" android:endColor="#8bc45d" android:startColor="#2f481c" />

        <stroke android:width="2dp" android:color="#999999" />

        <padding android:bottom="4dp" android:left="3dp" android:right="3dp" android:top="6dp" />

         <corners android:radius="10px"/>
    </shape></item>
<item><shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
        <gradient android:angle="90" android:centerColor="#6da23f" android:endColor="#8bc45d" android:startColor="#4c8a39" />

        <stroke android:width="1dp" android:color="#FFFFFF" />

        <padding android:bottom="4dp" android:left="3dp" android:right="3dp" android:top="6dp" />

        <corners android:radius="10px"/>
    </shape></item>

0

All Articles