ListView: Selectable Drawings of Different Sizes

I am using a custom ListView selector. Everything works fine, except that the drawings for state_focused and state_pressed are larger than the normal state.

Instead of the default implementation using 9-patch bitmaps, I use ShapeDrawables. They all use the same fill values ​​(see below). They seem to be overwritten by some other values. Probably the topic? Could not find it.

Listview

<ListView style="@style/dark_bg_style" android:id="@android:id/list" android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_margin="10dp" android:layout_centerInParent="true" android:drawSelectorOnTop="false" android:cacheColorHint="#00000000" android:layoutAnimation="@anim/list_layout_in" android:scrollbars="none" android:divider="#00000000" android:dividerHeight="5dp" android:listSelector="@drawable/list_sel" /> 

list_sel.xml

 <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:drawable="@android:color/transparent" android:state_window_focused="false"/> <item android:drawable="@drawable/button_bg_pressed" android:state_pressed="true"/> <item android:drawable="@drawable/button_bg_focused" android:state_focused="true" android:state_selected="true"/> <item android:drawable="@drawable/button_bg_focused" android:state_focused="true"/> </selector> 

excerpt from the list item layout

 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="horizontal" android:layout_width="fill_parent" android:layout_height="wrap_content" android:background="@drawable/menu_sel" > ... 

menu_sel.xml

 <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:drawable="@android:color/transparent" android:state_focused="true" android:state_selected="true"/> <item android:drawable="@android:color/transparent" android:state_selected="true"/> <item android:drawable="@android:color/transparent" android:state_pressed="true"/> <item android:drawable="@drawable/button_bg_normal"/> </selector> 

button_bg_normal.xml

 <shape xmlns:android="http://schemas.android.com/apk/res/android"> <solid android:color="#44C8C8C8" /> <corners android:radius="5sp" /> <padding android:left="10dip" android:top="5dip" android:right="10dip" android:bottom="5dip" /> </shape> 

button_bg_pressed.xml

 <shape xmlns:android="http://schemas.android.com/apk/res/android"> <solid android:color="#77C8C8C8" /> <corners android:radius="5sp" /> <padding android:left="10dip" android:top="5dip" android:right="10dip" android:bottom="5dip" /> </shape> 

button_bg_focused.xml

 <shape xmlns:android="http://schemas.android.com/apk/res/android"> <solid android:color="#44C8C8C8" /> <corners android:radius="5sp" /> <stroke android:color="#55FFFFFF" android:width="1dip" /> <padding android:left="10dip" android:top="5dip" android:right="10dip" android:bottom="5dip" /> </shape> 
+4
source share
1 answer

When working with ListView s, it is better to set the background states as android:listSelector . Your code has a conflict between the states in android:listSelector and android:background specified for a single line.

+2
source

Source: https://habr.com/ru/post/1390321/


All Articles