StateList for Compound ListView Row

203 Views Asked by At

When I click a row in my ListView the row is using the statelist but the individual elements are not.

  1. Setting duplicateParentState(true) on the image, text and image does nothing.
  2. Setting the individual items to clickable(true) applies the statelist changes to each item, but does not change the parent even if addChildStates() (approximation of methodname) is set.

So, I want to click a row in the ListView and have the states of the individual items updated. Obviously missing something.

I have the following row in a ListView:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/list_row"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:background="@drawable/statelist_row" >

    <FrameLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="0.1" >

        <ImageView
            android:id="@+id/product_image"
            style="@style/list_image"
            android:contentDescription="@string/product_image" />

        <ProgressBar
            android:id="@+id/product_image_progress"
            style="@style/list_image"
            android:visibility="gone" />
    </FrameLayout>

    <TextView
        android:id="@+id/product_name"
        style="@style/list_text"
        android:layout_weight="0.9"
        android:gravity="center_vertical" />

    <ImageView
        style="@style/list_caret"
        android:layout_weight="0.1"
        android:contentDescription="@string/caret" />

</LinearLayout>

In the adapter I make the row clickable:

    holder.productListRow.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Toast.makeText(context, "CLICKED: '" + holder.productName.getText().toString() + "'", Toast.LENGTH_LONG).show();
        }
    });

I have a statelist that is applied in the styles:

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

    <item android:drawable="@color/selected_item" android:state_pressed="true" android:textColor="@color/white" android:color="@color/white"/>
    <item android:drawable="@color/std_background" android:color="@color/black"/>

</selector>

From colors:

<color name="list_text_color">@drawable/statelist_produce_row</color>

Finally, from styles:

<style name="list_text" parent="fill_parent">
    <item name="android:textSize">@dimen/list_text_size</item>
    <item name="android:textColor">@color/list_text_color</item>
    <item name="android:layout_gravity">left|center_vertical</item>
    <item name="android:padding">@dimen/list_text_padding</item>
    <item name="android:textStyle">bold</item>
</style>
1

There are 1 best solutions below

0
On BEST ANSWER

Shouldn't you make the listview items clickable by setting an onItemClickListener for the listview instead of making each row clickable?

Try this

mListView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        ...
}        
});

or if you're extending the ListActivity, you can directly implement onListItemClick():

@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
    ...
}