Listselector Applies to the Entire List

ListSelector applies to the entire list

I had the same problem. I have a custom background image, and I don't want to have to make variants of that background image because that would be tedious to represent all the different states.

So I want to do the obvious thing, have a semi-transparent bar that is overlayed on top of the focused listitem and when the user taps the "enter" key or whatever, it flashes to the pressed overlay color which is more striking and somewhat more opaque.

The solution was to stay away from any @color or @drawable that refers to a color inside listSelector. I created two 3x3 pixel .png files. Each saved with the gamma layer. In my case it's two of the same color each mixed down in Gimp with a different transparency on the color layer. So when you select an item you get an overlay with 25% color, and when you press it you get a png with 50% color. I put them in my drawables as bg_list_item_pressed.png and bg_list_item_highlighted.png

Then I set my list selector to:

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

<!-- Selected -->
<item
android:state_focused="true"
android:state_pressed="false"
android:drawable="@drawable/bg_list_item_highlighted" /> <!-- @drawable/tab_focus -->

<!-- Pressed -->
<item
android:state_pressed="true"
android:drawable="@drawable/bg_list_item_pressed" /> <!-- @drawable/tab_press -->

</selector>

Then I added my listSelector attributes to my ListView in my layout xml:

android:listSelector="@drawable/list_selector"
android:drawSelectorOnTop="true"

Now it works exactly how I want it to work. Including using the D-pad to select a row, and click it with enter. Getting the highlighting and subsequent pressing colors exactly how they should be.

Android listSelector being applied to whole view rather than items

List selector is applied for the whole listview for this if you are inflating a view for listview row then apply that style for that total view layout.otherwise in your onListitemClick method it will give us a view apply your drawable there to the view by view.setBackgroundResource(resid)

Android list selector gets stuck when using Theme.Light

This is a pretty annoying issue that for some reason might be related to what theme you are using. I'm not sure exactly what the root cause is, but here is a workaround (present in the Android platform itself) that has worked for me.

<!-- Even though these two point to the same resource, have two states so the drawable will invalidate itself when coming out of pressed state. -->
<item android:state_focused="true" android:state_pressed="true"
android:drawable="@drawable/list_selector_background_transition" />
<item android:state_focused="false" android:state_pressed="true"
android:drawable="@drawable/list_selector_background_transition" />

(taken from list_selector_background.xml)

Notice that the Android platform developers seemed to have also run into this problem, and they have a workaround that re-uses the same background resource for different selector states. Apparently that triggers an re-validate call that fixes the problem (at least for me).

GridView listSelector on Gingerbread highlights entire grid

Apply the selector to the background of the item layout instead and set android:listSelector="@null".

Grid:

<FrameLayout
android:id="@+id/grid_container"
android:layout_width="match_parent"
android:layout_height="match_parent">
<GridView
android:id="@+id/categories_grid_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:horizontalSpacing="10dp"
android:numColumns="4"
android:listSelector="@null"
android:stretchMode="columnWidth"
android:verticalSpacing="10dp" />
</FrameLayout>

Item:

<FrameLayout
android:id="@+id/grid_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/list_view_selector">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</FrameLayout>

Android listSelector doesnt work

The cache color hint is an RGB color set by default to the window's background
color, that is #191919 in Android's dark theme. since the default cache color hint is #191919, you get a dark background behind each item during a scroll.To fix this issue, all you have to do is either disable the cache color hint optimization, if you use a non-solid color background, or set the hint to the appropriate solid color value. You can do this from code (see setCacheColorHint(int)) or preferably from XML, by using the android:cacheColorHint attribute. To disable the optimization, simply use the transparent color #00000000. :

if you need to change the list item color while pressing any List Item you have to add

android:listSelector="@drawable/listselector"

listselector.xml

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

<item android:state_focused="false" android:state_pressed="false">

<shape android:shape="rectangle">
<solid android:color="@android:color/transparent"/>

<stroke android:width="2dp" android:color="@android:color/transparent" />
<padding android:left="1dp" android:top="1dp" android:right="1dp"
android:bottom="1dp" />
</shape>
</item>

<item android:state_focused="true" android:state_pressed="false">

<shape android:shape="rectangle">

<solid android:color="@color/listitemfocus"/>
<stroke android:width="4dp" android:color="@android:color/transparent" />
<padding android:left="1dp" android:top="1dp" android:right="1dp"
android:bottom="1dp" />
</shape>
</item>

<item android:state_pressed="true">

<shape android:shape="rectangle">

<solid android:color="@color/listitempress"/>
<stroke android:width="4dp" android:color="@android:color/transparent" />
<padding android:left="1dp" android:top="1dp" android:right="1dp"
android:bottom="1dp" />
</shape>
</item>

</selector>

ListView item background via custom selector

I've been frustrated by this myself and finally solved it. As Romain Guy hinted to, there's another state, "android:state_selected", that you must use. Use a state drawable for the background of your list item, and use a different state drawable for listSelector of your list:

list_row_layout.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="?android:attr/listPreferredItemHeight"
android:background="@drawable/listitem_background"
>
...
</LinearLayout>

listitem_background.xml:

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

layout.xml that includes the ListView:

...
<ListView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:listSelector="@drawable/listitem_selector"
/>
...

listitem_selector.xml:

<?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/listitem_pressed" />
<item android:state_focused="true" android:drawable="@drawable/listitem_selected" />
</selector>

List selector not working properly

I assume after click on any item you close the drawer and show some screen. When drawer is closed listview resets itself and re-render it when drawer is opened again.

Solution would be to save the selected index and in getView method you set the desired bg color on the item for that index.

  1. Create a class level variable

    public static int NAV_CURR_SELECTION = 2; // you have used 2 in init()
  2. Add this

    NAV_CURR_SELECTION = position;

    in onItemClick() method

  3. Now, add these lines in onDrawerOpened()

    lvNavItems.setItemChecked(NAV_CURR_SELECTION, true);
    lvNavItems.setSelection(NAV_CURR_SELECTION);

Edit: Rather than using the two lines mentioned above in onDrawerOpened() do this. (Only change step 3. 1 and 2 need to remain same)

View v = lvNavItems.getChildAt(NAV_CURR_SELECTION);
v.setBackgroundColor(0xFF00FF00); // or whatever color you need


Related Topics



Leave a reply



Submit