Search code examples
androidandroid-listviewfocuspressed

Selector for Imageview in ListView


I have a problem with ImageView in my ListView. I want to change its drawable according the specified selector.

My ListView item is a simple LinearLayout, containing a TextView and an ImageView.

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal" 
    >

    <TextView
        android:id="@+id/rightMenuItemName"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:gravity="center_vertical|right"
        style="@style/right_menu_item_name"
        />

    <ImageView 
        android:id="@+id/rightMenuItemIcon"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="10dp"
        android:clickable="true"
        android:src="@drawable/selector_menu_item"/>

</LinearLayout>

The selector_menu_item drawable:

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

   <item android:state_pressed="true" android:drawable="@drawable/right_menu_s" />
   <item android:state_focused="true" android:drawable="@drawable/right_menu_s" />
   <item android:state_selected="true" android:drawable="@drawable/right_menu_s" />

   <item android:drawable="@drawable/right_menu" />

 </selector>

The ListView itself has his own selector for background coloring android:listSelector="...". The TextView has also a color-selector for the foreground color. This both selectors works like expected, only the image of the ImageView will not change.

My Adapter:

public class RightMenuAdapter extends BaseAdapter {

    …

    @Override
    public final View getView(int position, View convertView, ViewGroup parent) {
        int type = getItemViewType(position);
        if (convertView == null) {
            convertView = newView(type, parent);
        }
        bindView(position, type, convertView);
        return convertView;
    }



    @Override
    public View newView(int type, ViewGroup parent) {

        View v = inflater.inflate(R.layout.list_screens_right_menu, parent,
                false);
        v.setTag(new ViewHolder(v));

        return v;
    }

    @Override
    public void bindView(int position, int type, View view) {
        ScreenNavigationData item = items.get(position);

        ViewHolder vh = (ViewHolder) view.getTag();
        vh.name.setText(item.getMenuTitle());

    }


}

Any suggestions?


Solution

  • Damn, I found the solution:

    I have to remove the android:clickable="true" attribute from the ImageView. Now everything works ...

    Thank you anyway for your help!