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!
Question
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 3
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!
OTHER TIPS
change android:src="@ drawable/selector_menu_ item"/> to android:background="@ drawable/selector_menu_ item"/>
If you are overriding getView()
of your list adapter
surely this will work. Kindly note down, from your xml you are using same image only for all state.
This is only partial fix for the problem.
If you need to have click event added to ImageView
, then even if you set it in XML, it won't work.
What you need to do is set the android:duplicateParentState="true"
parameter in ImageView
XML, so it get working and you still can have the ImageView clickable.
My ImageView XML looks like so:
<ImageView
android:id="@+id/ivFavIcon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:src="@drawable/btn_history_fav"
android:duplicateParentState="true"
android:visibility="visible"
/>
and selector:
<?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/ic_hist_add_fav_highlighted"/>
<item android:state_pressed="false" android:drawable="@drawable/ic_hist_add_fav_normal"/>
</selector>
I spent few hours of checking different paths, hope it helps someone...