Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Grid view item not hightlight when press

My app has a grid view, By default, Its item does not highlight when I click on it (I don't know why? ). I try to add it a list selector as below, But it does not work too,

<GridView
                android:id="@android:id/list"
                android:layout_width="fill_parent"
                android:layout_height="match_parent"
                android:layout_above="@+id/load_more_process_id"
                android:layout_alignParentTop="true"
                android:drawSelectorOnTop="false"
                android:listSelector="@drawable/grid_selector"
                 >
            </GridView>

Here my selector:

<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:state_pressed="true" android:drawable="@android:color/black"></item>
<item android:state_pressed="false" android:drawable="@android:color/white"></item>

like image 439
Tai Tran Avatar asked Dec 02 '12 03:12

Tai Tran


2 Answers

If you have a focusable element in the list item contents (or the views for each grid item), the selector isn't drawn

For example, if the template for your list items is:

<LinearLayout android:orientation="horizontal" ... >
    <CheckBox android:id="@+id/checkBox" ... />
    <TextView android:id+"@+id/textView" ... />
</LinearLayout>

then ListView will not draw a selector for each ListView item, because the CheckBox is, by default, focusable.

You can either provide a background on each item that changes with selection state, or disable focus on all focusable elements (which in turn requires you to write a fairy fancy adapter to check the checkboxes when selection state changes.

eg:

<CheckBox android:id="@+id/checkBox" android:focusable="false" ... />

will cause an enclosing ListView to start drawing the selector again.

Example of a stateful drawable:

drawable/my_list_selector_bg.xml:

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

You then apply that to the background of each view returned by your adapter:

<LinearLayout android:orientation="horizontal" 
      android:background="@drawable/my_list_selector_bg"
  ... >

Either approach will work.

The list adpater classes (GridView, ListView, &c) call hasFocusable() on each view returned by the adapter, and disable selection drawing if hasFocusable() returns true. Fortunately, they also replicate the selection/focus/pressed/active state to the currently focused or selected adapter item, so you can draw it yourself if you want.

like image 189
Robin Davies Avatar answered Oct 15 '22 01:10

Robin Davies


For those of you who are having the same issue I was, try

gridView.setDrawSelectorOnTop(true);

I have an ImageView and a TextView in each of my GridView items. My selector was working, but it was drawing behind my image.

like image 31
wizurd Avatar answered Oct 15 '22 00:10

wizurd